1

I'm using Zend_Auth to identify a user in my application. This creates a session with the userobject.

My question is how do I make this object available to every Controller and action, so I don't have to pull it out of the session every time I need data from this object?

I'm guessing this should be done in bootstrap.php or index.php but I don't really know how to makte it available to every controller.. so any code examples would be appreciated!

Thanks!

1 Answers1

2

Bootstrap and index.php are probably too early. Since routing is not yet done, there is no controller into which to inject your $user object.

Several possibilities come to mind:

Use a BaseController

Create a BaseController form which all your other controllers extend. In the init() method of your base controller, you can place your assignment

$this->_user = Zend_Auth::hasIdentity() ? Zend_Auth::getIdentity() : null;

Then $this->_user will be available everywhere your controller actions.

But many people deride the idea of a BaseController. Better to use an Action Helper, described below.

Use an Action Helper

Create an action helper whose direct() method returns the $user object created in the same way as above. Then in your controllers, you can access it as $this->_helper->user.

Precisely what you name the helper class and where you place it are are dependent upon paths and namespaces you set in the Zend_Controller_Action_HelperBroker using the addPath() method.

Use a Controller Plugin

Listed for completeness. But really, the best thing IMO is an Action Helper above.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64