views:

341

answers:

3
public function init(){
     $this->view->user = Zend_Auth::getInstance()->getIdentity();
     $this->view->siteName = Zend_Registry::get('config')->site->name;
     $this->view->menu = $this->_helper->generateMenu(Zend_Auth::getInstance()->getIdentity());
     $this->view->slogan = Zend_Registry::get('config')->site->slogan;
    }

This is the init file in all of my controllers across all modules, is there a place I can put this code so it executes every request irregardless of the module/controller being called?

+2  A: 

You can extend Zend_Controller_Action:

public class My_Controller_Action extends Zend_Controller_Action
{
    public function init()
    {
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
        $this->view->siteName = Zend_Registry::get('config')->site->name;
        $this->view->menu = $this->_helper->generateMenu(Zend_Auth::getInstance()->getIdentity());
        $this->view->slogan = Zend_Registry::get('config')->site->slogan;
    }

}

Then you just change your controllers to extend My_Controller_Action rather than Zend_Controller_Action. Just keep in mind that if you need to add additional code to the init method of a controller, you'll have to invoke parent::init() as well:

public class FooController extends My_Controller_Action
{
    public function init()
    {
        parent::init();

        // Do something.
    }

    public function IndexAction()
    {
        // ...
    }
}
Emil H
+5  A: 

I'd rather advise you to write a plugin by extending Zend_Controller_Plugin_Abstract, it is its purpose.

By this way, you will have no need to do anything anywhere in your controller. Then you can use the registry to access to your data...

class My_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
    protected $_auth = null;

    protected $_acl = null;

    public function __construct (Zend_Auth $auth, Zend_Acl $acl)
    {
        $this->_auth = $auth;
        $this->_acl = $acl;
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
         //some code
    }
}

And then in your bootstrap.php

$this->_front->registerPlugin(new My_Controller_Plugin_Layout());

http://framework.zend.com/manual/en/zend.controller.plugins.html

Boris Guéry
+2  A: 

To share code across controllers, create an Action Helper which was designed primarily to solve the problem you have.

They can be run "on demand":

$myHelper = $this->_helper->MyHelper;
$myHelper->someFunction();

and also have a set of hooks that the dispatch process will call automatically. To use the hooks, you need to register the action helper with the broker:

$helper = new App_Controller_Action_Helper();
Zend_Controller_Action_HelperBroker::addHelper($helper);

The available hooks are:

  • init()
  • preDispatch()
  • postDispatch()

For more info,the manual page can be found at http://framework.zend.com/manual/en/zend.controller.actionhelpers.html and I have written a couple of articles about them: http://akrabat.com/2008/10/31/using-action-helpers-in-zend-framework/ and http://akrabat.com/2008/11/05/hooks-in-action-helpers/

Rob Allen