Re: [fw-general] How do I factor out tasks common to every action controller?
-- 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/
[fw-general] How do I factor out tasks common to every action controller?
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?
Re: [fw-general] How do I factor out tasks common to every action controller?
you could do it in your bootstrap. 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? -- View this message in context: http://www.nabble.com/How-do-I-factor-out-tasks-common-to-every-action-controller--tp20003502p20004290.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] How do I factor out tasks common to every action controller?
I created my own controller, derived from Zend_Controller_Action, calling it Tim_Controller_Action and all of my controllers derive from my controller. (putting it in Library/Tim/Controller/Action.php) Dont forget to call parent::init(); if you override the init in the real controllers. On Thu, Oct 16, 2008 at 10:15, gerardroche [EMAIL PROTECTED] wrote: you could do it in your bootstrap. 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? -- View this message in context: http://www.nabble.com/How-do-I-factor-out-tasks-common-to-every-action-controller--tp20003502p20004290.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] How do I factor out tasks common to every action controller?
Sorry my mistake, I need to learn how to read better ;) On Thu, Oct 16, 2008 at 12:04, Tim Nagel [EMAIL PROTECTED] wrote: I created my own controller, derived from Zend_Controller_Action, calling it Tim_Controller_Action and all of my controllers derive from my controller. (putting it in Library/Tim/Controller/Action.php) Dont forget to call parent::init(); if you override the init in the real controllers. On Thu, Oct 16, 2008 at 10:15, gerardroche [EMAIL PROTECTED]wrote: you could do it in your bootstrap. 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? -- View this message in context: http://www.nabble.com/How-do-I-factor-out-tasks-common-to-every-action-controller--tp20003502p20004290.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] How do I factor out tasks common to every action controller?
Out of curiosity, why the resistence to subclassing the action controller? -Matt On 10/15/08, Steven Szymczak [EMAIL PROTECTED] 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?
Re: [fw-general] How do I factor out tasks common to every action controller?
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.