0

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 );

1 Answer 1

2

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.

3
  • I added the observer in the <frontend> tag and the code is calling after the review save event, but I tried adding the $request->sendResponse() after the setRedirect() which you mentioned, but no luck, the rest of the controller code gets executed. I tried putting exit(), the reviews are not saving.
    – Dinesh
    Commented Jul 12, 2013 at 12:24
  • In most cases return; is good enough. Don't call exit or die - makes testing difficult.
    – philwinkle
    Commented Jul 12, 2013 at 17:52
  • 1
    return will not work in this case, because the remaining execution scope may set a different redirect header on the response object before it is sent by the Front Controller.
    – benmarks
    Commented Jul 12, 2013 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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