On 3 Jul 2008, at 12:51, AJ McKee wrote:
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.

Does anyone have any pointers to me about how I would accomplish this,
if its possible.

Hi AJ,

One thing you **could do** is extend Zend_Controller_Action in order to override the constructor.

(You then have your application controllers extend from that).

<?php
class My_Controller_Action extend Zend_Controller_Action
{
        // shared class properties here

        public function __construct(Zend_Controller_Request_Abstract $request,
                                                         
Zend_Controller_Response_Abstract $response,
                                                         array $invokeArgs = 
array()
                                                        )
        {
                // stuff to happen before init()
                
                parent::parent::__construct($request, $response, $invokeArgs);

                //stuff to happen after init()
        }


        // Class Continues...
}

The key thing is to call parent::_construct() which runs init() as its last action. Also you can't access things like $this->getRequest () until after you've done so (however they are already in scope so you may not want to...)

I use this approach myself for loading module include paths. In general though I'd want to avoid loading too much into the controller this way as plugins and action helpers are more flexible in the main.

(Note to self: move module include path stuff to a plugin! :-)

I hope this helps.

regards,
Carlton

Reply via email to