views:

42

answers:

1

Hi,

I am a newbie to Zend framework, I want to know how can we restrict the call to predispatch() function in my controller for any particular action.

-DevD

+2  A: 

In your controller try

public function preDispatch()
{
    if($this->getRequest()->getActionName() === 'actionName') {
        return; // ignoring preDispatch
    }
    // run preDispatch code when not actionName
}

The preDispatch method is called before any controller Actions are called in the MVC Request Lifecyle. Thus, you cannot disable preDispatch from an individual action.

You can create a property inside your controller or a variable in the preDispatch method where you put in the action names (without the Action suffix) you want preDispatch to return from without doing anything. In the example code above, you wouldn't test against one action name but against the list of action names, probably with in_array.

See http://devzone.zend.com/article/11978-Zend-Framework-MVC-Request-Lifecycle

Gordon
There is nothing built-in for this purpose. The approach with the nopredispatchaction array sounds perfectly valid to me, as does your current approach. I probably wouldn't place the custom method in the controller though, but make it into an action helper, so you can reuse it in any controller. Action Helpers can be called in preDispatch.
Gordon
Hi Gordon, Thanks for the reply. I did a bit of research and figured out that there doesn't exist any build in feature in ZF for this.secondly on actionhelpers, Instead of putting it under action helpers what I have done is, I have created an applicationController (Zend_Controller_Action) and extended rest of my app controllers from my app controller. this app controller holds all the function common across my other controllers.though I am a bit newbie to ZF, will get over it in few days :)