I would extend Zend_Controller_Action and make an application wide
action controller that has the init function which centralizes all the
duplicated code.
btw, having the Database connection in the Controller is not best practice at
all. You have to separate controllers and model, hence it being MVC not VC. I
suggest a Service Layer between them or Model classes that have an public API
that says nothing about Databases and stuff.
greetings,
Benjamin
On Friday 23 January 2009 21:08:14 jasonzfw wrote:
> 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...
--
Benjamin Eberlei
http://www.beberlei.de