【问题标题】:Magento Redirect from Observer来自观察者的 Magento 重定向
【发布时间】:2013-05-14 05:04:12
【问题描述】:

我在 Magento 中从观察者创建工作重定向时遇到问题。除此之外,我需要了解为什么像我们在控制器中所做的异常在观察者中不起作用。

在控制器中完成的典型异常如下(adminhtml 控制器)

$message = $this->__('Exception Message.');
Mage::getSingleton('adminhtml/session')->addError($message);
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;

在博客的某处,我读到了以下从观察者重定向的方法。

Mage::getSingleton('core/session')->addError('Exception Message.');
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
Mage::app()->getResponse()->sendResponse();
exit;

我不明白使用观察者和控制器进行重定向时的基本区别。

为什么控制器重定向在观察者中使用时不起作用。

请帮我解释一下。

非常感谢。

【问题讨论】:

  • 我想这就是你要找的东西:stackoverflow.com/questions/9749005/…
  • $observer->getRequest()->setParam('return_url','google.com/');引用 URL 中的上述代码不起作用。
  • 怎么样:$this->getResponse()->setRedirect($returnUrl); (而不是 getFrontController())
  • ^调用未定义的方法 MongCars_Checkout_Model_Observer::getResponse()

标签: magento


【解决方案1】:

见下面的链接,我也发布了代码 它可能会帮助你。

来源:http://aaronbonner.io/post/418012530/redirects-in-magento-controllers

Magento 控制器中的重定向 在 Zend Framework 控制器中,要输出 html 以外的内容,您需要禁用 ViewHelper 默认值以及 ZF 通常编织的其他一些魔法。

在 Magento 中,执行控制器重定向时也是如此。

我注意到在 PHP 5.2 上重定向似乎被忽略了,而我的 Macports 5.3 设置它可以工作。原来我错过了一个技巧,因此在 Magento 中正确重定向如下:

MyPackage/MyModule/controllers/MyController.php

$this->_redirectUrl($this->getUrl('http://www.someurl.com/someresource'));  
$this->setFlag('', self::FLAG_NO_DISPATCH, true);  
return $this;

如果没有 setFlag 调用,您的重定向将被忽略。返回调用停止任何进一步的代码执行,并看到您的重定向被执行。

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 2014-10-28
    • 2013-08-11
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    相关资源
    最近更新 更多