Magento – Observer – Stop controller action after review_save_after

event-observerfrontendmagento-1.7review

I am using Magento 1.7.0.2. I need to prevent ratings in the frontend from saving after the reviews has been saved. So I have used the <reveiw_save_after> event in order to achieve it. But I am unable to stop the controller action from happening after the save event. Here is my code:

etc\config.xml

    <events>
        <review_save_after>
            <observers>
                <rating_prevent_observer>
                    <type>singleton</type>
                    <class>MyModule_Rating_Model_Observer</class>
                    <method>preventAddRatings</method>
                </rating_prevent_observer>
            </observers>
        </review_save_after>
    </events>

Rating\Model\Observer.php

     class MyModule_Rating_Model_Observer
     {
        function preventAddRatings(Varien_Event_Observer $observer)
        {
            $app = Mage::app();
            $request = $app->getRequest();

$app->getFrontController()->getResponse()->setRedirect($request->getBaseUrl() . $request->getPathInfo());
        }
    }

The observer is calling but the controller action is still executing. Is this the correct way to prevent the controller action?.

Thanks.

Update:

I tried using the below code but I am unable to get the controller action from the observer object.


$controller = $observer->getControllerAction();
$controller->getRequest()->setDispatched(true);
$controller->setFlag(
'',
Mage_Core_Controller_Front_Action::FLAG_NO_DISPATCH,
true
);

Best Answer

After setting the redirect you need to call the response object's sendResponse() method and terminate execution.

Note that your observer needs to be configured under the frontend event area, as it would not be appropriate for this redirect to occur in the admin.