-- AJ McKee <[EMAIL PROTECTED]> wrote
(on Thursday, 03 July 2008, 12:51 PM +0100):
> Please excuse my ignorance here.
>
> I have several Controllers that all do various things. However there
> are common things I wish them all to do. Mostly just set up vars for
> ease of use. Eample
>
> protected $_myRegistry = null;
> protected $_myDebug = false;
>
> public function init()
> {
> $this->_myRegistry = $this->_registry = Zend_Registry::getInstance();
> %this->_myDebug = $this->_myRegistry->get('debug');
> }
>
> I am trying to apply the principle of DRY here, because as the
> application grows, I am repeating myself more and more.
>
> I created an action plugin thinking I was on the right track, however
> I discovered I was not. Basically, I want all this stuff setup before
> my controller is called, but have it available to my controller in a
> manner similar to $this->_myRegistry. Or would I best be doing this in
> an action helper?
Action Helper is the way to go here. An action helper has introspection
into the action controller, and its primary purpose is to push all those
bits of code you need to re-use to a common place so that they can be
used on-demand by any controller.
Additionally, you can have action helpers listen on controller
intialization and pre/postDispatch() events; this can be useful for
injecting variables into your action controllers. The variables will
need to be public, obviously, but there are very few cases where this
should be an issue.
> Does anyone have any pointers to me about how I would accomplish this,
> if its possible.
I've written a tutorial on action helpers on DevZone:
http://devzone.zend.com/article/3350-Action-Helpers-in-Zend-Framework
that should serve as a good starting point.
--
Matthew Weier O'Phinney
Software Architect | [EMAIL PROTECTED]
Zend Framework | http://framework.zend.com/