4

I would like to write some custom code before any actions in a controller is executed. It would seem like an easy one, but i am unable to find help anywhere in google. I tried to override preDispatch() function in my controller, but it ends up getting weird (instead of showing my page, it shows the default page without any content in it)

Its a admin controller and i need to do some custom validation before any action in it is executed. Based on the validation, the request may be forwarded to some other controller.

7

Good news: there's no need to rewrite preDispatch! There is dynamic eventing that is fired both before and after the dispatch:

An excerpt of what is required to tap into the event in your module's etc/config.xml file:

<global>
    <events>
        <controller_action_predispatch_catalog_product_view>
            <observers>
                <yourmodule_capcpv>
                    <class>YourCompany_YourModule_Model_Observer</class>
                    <method>catalogProductViewPredispatch</method>
                </yourmodule_capcpv>
            </observers>
        </controller_action_predispatch_catalog_product_view>
    </events>
</global>

And your observer would look like the following:

app/code/local/YourCompany/YourModule/Model/Observer.php:

<?php

class YourCompany_YourModule_Model_Observer
{
    public function catalogProductViewPredispatch($observer)
    {
        //get the controller action method
        $controller = $observer->getEvent()->getControllerAction();

        //log something
        Mage::log('Testing - I made it to the predispatch of the catalog product viewAction!');

        //get things from the request:
        $request = $controller->getRequest();

        //set things on the response:
        $reponse = $controller->getResponse();
        $response->setBody('some stuff');
    }
}
  • thanks. bt wats the event for before all the actions? – shahalpk Oct 22 '13 at 17:18
  • to run something before all actions, use controller_action_predispatch – philwinkle Oct 22 '13 at 17:23
  • but won't that be fired before all the actions in every controller? i juz need to execute this for a single controller. – shahalpk Oct 22 '13 at 17:28
  • Oh, I didn't understand that. You can just use controller_action_predispatch_ and add the route name, e.g. catalog or customer. – philwinkle Oct 22 '13 at 17:32
  • thats for all the controllers in a particular module. I checked the preDispatch() method in Varien/Action.php, dont think there is an option for individual controllers. maybe i'll just use the above event and filter the controller in the observer D: – shahalpk Oct 22 '13 at 17:38
0

here is an example of Customer->AccountController. In the controller you have override mention mention predispatch function as mentioned below:

public function preDispatch()
{
$action = $this->getRequest()->getActionName();
$ExitsopenActions = array(
'create',
'login',
'logoutsuccess',
'forgotpassword',
'forgotpasswordpost',
'changeforgotten',
'resetpassword',
'resetpasswordpost',
'confirm',
'confirmation'
);
$newOpenAction=array('passwordreset');
$allActions=array_merge($ExitsopenActions,$newOpenAction);
$Custompattern = '/^(' . implode('|', $newOpenAction) . ')/i';

if (preg_match($Custompattern, $action)) {
$this->getRequest()->setActionName('create');
}
parent::preDispatch();

if ($action != $this->getRequest()->getActionName())
{
$this->getRequest()->setActionName($action);
}
if (!$this->getRequest()->isDispatched()) {
return;
}
$mypattern = '/^(' . implode('|', $allActions) . ')/i';
if (!preg_match($mypattern, $action)) {
if (!$this->_getSession()->authenticate($this)) {
$this->setFlag('', 'no-dispatch', true);
}
} else {
$this->_getSession()->setNoReferer(true);
}
}

$ExitsopenActions consists of all the actions that are there in the native.

$newOpenAction consists of all the new actions you want to add.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.