I have my application within Zend Framework 1. I'm using Zend_Auth to manage sessions. Below is how I check authentication within the IndexController class:
class IndexController extends Zend_Controller_Action
{
public function init()
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$this->view->user = $auth->getIdentity();
}
}
public function indexAction()
{
}
}
Basically it just sets a view variable of user to whatever is in the auth object. Within my view I can check if the user variable is set and act appropriately (eg Display "Welcome Tom!" and a logout link)
However, this functionality is not available yet in my other controllers. Rather than duplicate the same code within each init() method, how can I do this? I'm not so sure where to put the code for this.
UPDATE:
I tried to do something like this in the bootstrap file:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initView() {
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$this->view->user = $auth->getIdentity();
}
}
}
..but I get the following error: Notice: Indirect modification of overloaded property Bootstrap::$view has no effect in /var/www/budgetz/application/Bootstrap.php on line 9 Warning: Creating default object from empty value in /var/www/budgetz/application/Bootstrap.php on line 9
.
You can use an abstract class inheriting from Zend_Controller_Action
like this:
abstract Class Yourlibrary_Controller_ControllerAbstract extends Zend_Controller_Action
{
public function preDispatch()
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$this->view->user = $auth->getIdentity();
}
}
}
And yours Controllers inherit Yourlibrary_Controller_ControllerAbstract
not Zend_Controller_Action
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.