-- Steven Szymczak <[EMAIL PROTECTED]> wrote
(on Wednesday, 15 October 2008, 11:09 PM +0100):
> 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...
Create an action helper that implements functionality in preDispatch(),
and register it with the helper broker in your bootstrap. It might look
like this:
class My_CommonViewVariables extends Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
$view = $this->getActionController()->view;
$viewCfg = Zend_Registry::get('SITE_CFG')->dirs;
$view->assign(array(
'pubImages' => $viewCfg->images,
'jsDir' => $viewCfg->js,
'cssDir' => $viewCfg->css,
));
}
}
And then, in your bootstrap:
Zend_Controller_Action_HelperBroker::addHelper(new My_CommonViewVariables);
BTW, don't call magic methods directly; it's a bad idea. Your original
example would have been better written using "$this->view->cssDir =
$view_cfg->dirs->css;", or, if you really want to use method calls, the
assign() method (which takes either two arguments, like __set(), or an
array of key/value pairs, as in the helper I detail above).
--
Matthew Weier O'Phinney
Software Architect | [EMAIL PROTECTED]
Zend Framework | http://framework.zend.com/