2013-05-14 86 views
1

我無法在observer.Apart中創建Magento中的工作重定向,因此我需要理解爲什麼異常就像我們在控制器中執行的操作在Observer中不起作用。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; 

我不明白在使用觀察器和控制器時的基本重定向差異。

爲什麼在觀察者中使用控制器重定向不起作用。

請幫我解釋一下。

非常感謝。

+0

我認爲這是你要找的東西:http://stackoverflow.com/questions/9749005/magento-redirect-customer-from-observer-method – JNDPNT 2013-05-14 07:05:28

+0

$ observer-> getRequest() - > setParam(' return_url」,的 'http://www.google.com/'); 以上代碼來自引用的URL不起作用。 – Slimshadddyyy 2013-05-14 12:17:47

+0

那麼:$ this-> getResponse() - > setRedirect($ returnUrl); (而不是getFrontController()) – JNDPNT 2013-05-14 12:25:01

回答

0

看到下面的鏈接,我也發佈了代碼 它可能會幫助你。

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

重定向在Magento控制器 在Zend框架控制器,以HTML以外的東西輸出你將要與其他一些神奇的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電話,您重定向將被忽略。返回調用會停止任何進一步的代碼執行,並看到您的重定向被執行。