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.