Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 872911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:51:17+00:00 2026-05-15T10:51:17+00:00

I am working on a web application which requires the user to login before

  • 0

I am working on a web application which requires the user to login before they see or do anything. No part of this app should be accessible without being logged in. (Except of course, the login controller)

Currently I am using sessions to handle the authentication and I have put code in each controller in the init() function to check if their session is valid.

This was a temporary workaround, but it is redundant and inefficient.

I would like my init() function to be similar to the following, but I am not sure how to achieve it:

public function init()
{
    // If user not logged in redirect to login controller
    $myLibrary = Zend_Library_MyLibrary();
    $myLibrary->CheckAuth();
}

So my question really has two parts:

  1. Where is the best place to store code that will be used in multiple controllers?
  2. How do I then call that function from a controller?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:51:18+00:00Added an answer on May 15, 2026 at 10:51 am

    Code that is reused across multiple controllers is best placed into an ActionHelper. However, for your case, I suggest to write a Controller plugin. Those hook into the Dispatch process at various stages:

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        if(!Zend_Auth::getInstance()->hasIdentity())
        {
            $request->setControllerName('auth');
            $request->setActionName('login');
            // Set the module if you need to as well.
        }
    }
    

    The above assumes you are using Zend_Auth to authenticate and manage your user identities.

    You want a plugin over a helper, because checking if the user is logged in should happen automatically, without you having to call a checkAuth() method somewhere. Of course, nothing stops you to add an ActionHelper too, e.g.

    class My_Helper_CheckAuth extends Zend_Controller_Action_Helper_Abstract
    {
        public function checkAuth()
        {
            return Zend_Auth::getInstance()->hasIdentity();
        }
        public function direct()
        {
            return $this->checkAuth();
        }
    }
    

    Once you registered your helper in the bootstrap, you could use it in every controller to check if the user is already authenticated:

    if ( $this->_helper->checkAuth() === FALSE) {
        // do something
    }
    

    Also see these tutorials:

    • Front Controller Plugins in Zend Framework
    • Action Helpers in Zend Framework
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a web app which requires a standard desktop application
We are working on a GWT web-application which requires secure user authentication. We have
I have a working web application which already has a login and registration system.
I'm working on a Asp.Net 3.5 Web Application which requires Async Web service calls.
I am working on a web application which must be able to encrypt data
I have one web application which I have been working for some time.I am
I'm working on a web based application which uses a JSON over HTTP based
I'm working on an MVC3 Razor web application which gets it's page decoration from
Well, simple question. I'm working with VS2008 on an ASP.NET web application which has
I am working on a web application (using Grails) which will generate a gift

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.