Steven Szymczak wrote:
> 
> Every one of my action controllers has an init() function that pulls a 
> config object from the registry and uses it to set some paths used 
> throughout the site (e.g.):
> 
> public function init() {
>       $view_cfg = Zend_Registry::get('SITE_CFG');
> 
>       $this->view->__set('pubImages', $view_cfg->dirs->images);
>       $this->view->__set('jsDir', $view_cfg->dirs->js);
>       $this->view->__set('cssDir', $view_cfg->dirs->css);
> }
> 
> How can factor out this functionality without resorting to a parent 
> class for all my action controllers?  I seem to recall a similar post to 
> the list, several months ago, that suggested utilizing a helper somehow...
> 
> Suggestions?
> 
> 

Another option would be to implement a Controller Plugin:
http://framework.zend.com/manual/en/zend.controller.plugins.html

class App_Controller_Plugin_SiteConfig extends
Zend_Controller_Plugin_Abstract
{
    protected $_view;

    public function __construct(Zend_View_Abstract $view)
    {
        $this->_view = $view;
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $view_cfg = Zend_Registry::get('SITE_CFG');

        $this->_view->__set('pubImages', $view_cfg->dirs->images);
        $this->_view->__set('jsDir', $view_cfg->dirs->js);
        $this->_view->__set('cssDir', $view_cfg->dirs->css);
    }
}

Which gets added to your front controller in your bootstrap:

    $frontController->registerPlugin(new
App_Controller_Plugin_SiteConfig($view));

The SITE_CFG values would be set before every action (preDispatch).

This is probably overkill for your need - you could just set your view
objects in your bootstrap directly as gerardroche suggests. I just wanted to
point it out, since it is handy for doing things at certain points in the
controller process lifetime (routeStartup, postDispatch, etc.), and is an
alternative to subclassing the action controller.
-- 
View this message in context: 
http://www.nabble.com/How-do-I-factor-out-tasks-common-to-every-action-controller--tp20003502p20007516.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to