Hi Matthew & Carlton,
Thanks for the suggestions. I decided in the end to use an action
helper and preDispatch to do this. So here is my current idea on it.
So far so good it seems to work;
1. Create and action helper
<?php
class My_Controllers_Helpers_ActionSetup extends extends
Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
$this->_actionController->_MyCoolAppsRegistry =
Zend_Registry::getInstance();
}
}
2. Register the helper in my bootstrap
// N.B. This class is stored in /library/My/Controllers/Helpers/ActionSetup.php
Zend_Controller_Action_HelperBroker::addHelper(new
My_Controllers_Helpers_ActionSetup());
3. Use it in a Controller
As I am using preDispatch, its run before my controller action is
setup, therefore the vars should be already registered and setup for
me
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
Zend_Debug::dump($this->_MyCoolAppsRegistry);
}
}
Done. I think
If you get a chance you may like to let me know if I got that all
sorted out correctly.
Cheers
AJ
.
On Thu, Jul 3, 2008 at 1:17 PM, Matthew Weier O'Phinney
<[EMAIL PROTECTED]> wrote:
> -- 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/
>