Hi,
Many many thanks for the great set of responses. This helps to clarify
nicely. I've created a custom action helper, integrated it into the site,
and confirmed the framework is able to recognize and execute it. However
there remains one perplexing matter: how do I initialize this action helper
in a way that makes several of its variables automatically available to all
controllers? The idea is to completely remove all redundant code from the
various controllers' init() methods, thereby making the variables within the
controller available in each controller's $this context.
Based on your varied responses regarding the action helper, surely there's
some way to do this? The goal is to use this action helper to make the
following variables automatically available to all controllers:
$this->config = Zend_Registry::get('config');
$this->cache = Zend_Registry::get('cache');
$this->db = Zend_Registry::get('db');
Thanks for all of your help!
Jason
Ionut Gabriel Stan-2 wrote:
>
> On 1/8/2009 12:05, Rob Allen wrote:
>>
>> On 8 Jan 2009, at 09:26, Bart McLeod wrote:
>>
>>> You can of course have a base custom controller, but you do not need to.
>>>
>>> Depending on what you need exactly you can use either an action helper
>>> in the init method or a plugin or both a plugin and an action helper.
>>>
>>> public function init(){
>>> $this->_helper->myInit(); //instead of your six lines of code
>>> }
>>
>>
>> Actually, if you have a My_Controller_Action::init(), you don't need to
>> define and init() at all in the child classes, whereas if you use a
>> helper, you do need to define the init() in the controllers where you
>> need the myInit().
>
> Depends on the place where you register the helper, because
> HelperBroker::__construct() will call the init() method of all
> registered helpers at the time Zend_Controller_Action is instantiated.
> Then, the same HelperBroker will call the preDispatch() and
> postDispatch() method of all helpers before and, respectively, after
> Zend_Controller_Action::dispatch().
>
> There's a nice representation of this process here:
>
> http://surlandia.com/wp-content/uploads/2008/11/zf-dispatch-lifecycle-bw.jpg
>
> So, IMHO, a helper could be the solution, the only issue remaining might
> be the place to register the helper and then what callback method to
> use, init() or preDispatch().
>
>
>>
>> Regards,
>>
>> Rob...
>>
>
>
--
View this message in context:
http://www.nabble.com/Best-Practices%3A-Eliminate-redundancy-within-various-controller-init-methods--tp21341232p21631746.html
Sent from the Zend Framework mailing list archive at Nabble.com.