Hi Matthew,

init() happens at object instantiation, which happens before
preDispatch() -- it's basically provided so that you don't need to
override the constructor, and thus possibly forget to set the request,
response, and other parameters (as you discovered the hard way).

It does seem like having init() as a means to bypass the constructor and also having preDispatch() is possibly a little unintuitive. Surely init() is a constructor of sorts?

In theory I can set the request and response....

$controller->setRequest( new Zend_Controller_Request_Http );
$controller->setResponse( new Zend_Controller_Response_Http );

...as I do the router...

$controller->setRouter($router);

...but it will still be required to specified in the constructor of classes overriding __construct...

parent::__construct( new Zend_Controller_Request_Http, new Zend_Controller_Response_Http );

...however at that point the parameters will not be available.

It seems that if we have to follow the init() path the manual is going to have to say something like...

"If you subclass Zend_Controller_Action you should not override the constructor instead setup code should go in the init() method"

In other words it will need to be a warning. That is where I feel that init() is unintuitive and most people (I say with hesitation being a self elected representative) would expect to go with the constructor for this task.

Note also that I didn't forget to set the request and response objects when I overrode the constructor, rather that when I did set them in the subclass no parameters were available in the controller.

Theoretically, if something goes wrong during the initialization
sequence, you could throw an exception, in which case the preDispatch()
will not be called -- this may be handy so that you can, for instance,
setup your models, etc., and, if unavailable, prevent the action from
dispatching. preDispatch() might then actually use the models to do some
determination of whether or not to skip the current action.

I follow the theory and I'd be interested in hearing how others are using preDispatch and whether their use really does differentiate init () from preDispatch() in the way you mention.

Anyway, hope I sound curious rather than whining...

Nick

--------------------------------------------
Ingredients Australia

http://www.ingredients.com.au
--------------------------------------------

Reply via email to