如何从Magento的Observer重定向

时间:2013-11-26 17:22:31

标签: magento redirect magento-1.7 observers

我正在使用观察者来捕获该事件:controller_action_postdispatch_checkout_onepage_saveBilling

我希望该事件能够检查客户的帐单地址。

我的问题是:如何在此时向用户发送消息。按下继续按钮时。

我使用了:Mage::getSingleton('core/session')->addError('My message');

但是在刷新页面之前不会显示该消息。这是第二个问题。 如何从该点重定向页面? 我没有运气使用:$this->_redirect('*/*/');和没有运气的硬编码网址。 理想情况下,当客户按下继续按钮时,我希望在进行一些检查后,通过消息停止该过程。检查观察者,它的工作正常。

希望它有意义......

修改 这是我的代码。只是为了确保我做得对: 型号/观察

class Company_Restrictions_Model_Observer {

    public function notifyUser($observer) {
//Lot's off lines are tested here.Nothing seems to work..
$observer->getRequest()->setParam('return_url','http://www.google.com/');

}
}

config.xml中

<?xml version="1.0"?>

<config>
  <modules>
      <Company_Restrictions>
          <version>0.1.0</version>
      </Company_Restrictions>
  </modules>
  <global>
    <models>
        <company_restrictions>
             <class>Company_Restrictions_Model</class>
        </company_restrictions>
    </models>
    <helpers>
        <company_restrictions>
             <class>Company_Restrictions_Helper</class>
        </company_restrictions>
    </helpers>
    <events>
      <controller_action_postdispatch_checkout_onepage_saveBilling>
        <observers>
          <notify_user>
            <type>singleton</type>
            <class>Company_Restrictions_Model_Observer</class>
            <method>notifyUser</method>
          </notify_user>
        </observers>
      </controller_action_postdispatch_checkout_onepage_saveBilling>     
    </events>
  </global>
</config>

再次编辑

在调试后我发现很多函数都给了我错误。未定义的方法...对于ex:_redirect()或Response()或Body()..有谁知道为什么?

4 个答案:

答案 0 :(得分:0)

看看@ Magento - Redirect Customer from Observer Method

public function moduleMethod(Varien_Event_Observer $observer)
{
    $observer->getRequest()->setParam('return_url','http://www.google.com/');
}

答案 1 :(得分:0)

我有一个成功重定向的Observer,我只是使用setRedirect方法:

public function moduleMethod(Varien_Event_Observer $observer)
{
    // get the current front controller
    $frontController = $observer->getEvent()->getFront();

    if(.....)
    {           
         $newUrl = ...;

         $frontController->getResponse()->setRedirect($newUrl);
         Mage::app()->getResponse()->sendResponse();
         exit;
    }
}

答案 2 :(得分:0)

仅供参考,我会给你从stackoverflow magento得到的答案: 这就是我设法让它发挥作用的方式..

$controllerAction = $observer->getEvent()->getControllerAction();
$result = array();
$result['error'] = '-1';
$result['message'] = 'YOUR MESSAGE HERE';
$controllerAction->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

Magento Stackoverflow

答案 3 :(得分:0)

此代码可以使用:

$url = YOUR_URL; // e.g. Mage::getUrl('customer/account/login')
Mage::app()->getResponse()
            ->setRedirect($url)
            ->sendResponse();