问题描述
在此事件 checkout_cart_add_product_complete
中,我希望将客户重定向到外部网页 http://www.example.com/
.为此,我正在使用此代码,它根本不起作用:-
In this event checkout_cart_add_product_complete
, I want the customer to be redirected to an external web page http://www.example.com/
. For this I am using this code, which is not working at all:-
public function moduleMethod() {
/* @var $response1 Mage_Core_Controller_Response_Http */
$response1 = $observer->getEvent()->getResponse();
/* @var $response2 Mage_Core_Controller_Response_Http */
$response2 = Mage::app()->getResponse();
$url = 'http://www.example.com/';
$response1->setRedirect($url);
return;
}
我对这两个变量$response1
"和$response2
"都使用了setRedirect()
"方法,但是他们中的一些人向我展示了购物车页面,而我想查看此页面 http://www.example.com/
.
I have used the "setRedirect()
" method on both these variables "$response1
" and "$response2
", but both of them show me the Shopping Cart page, whereas I want to see this page http://www.example.com/
instead.
我想要的:
- 当我可以有效地使用事件观察器进程时,我不想覆盖控制器类,只是为了重定向客户.
- 我不想使用 PHP 内置函数
header()
",因为 Magento 框架以一种有效的方式提供了此功能.
- I don't want to override the Controller Class, just to redirect the Customer, when I can effectively use the Event Observer process.
- I don't want to use the PHP in-built function "
header()
", when Magento framework provides this functionality in an efficient way.
推荐答案
tl;dr: 底部正确的观察者代码.
tl;dr: Correct observer code at the bottom.
我回答之前的注意事项:确保观察者正在被触发;单步执行您的代码或使用 die('here');
.正如所写,您的示例方法没有正确的原型来接收事件观察者数据(缺少参数).
A note before I answer: Make sure that the observer is being triggered; step through your code or use die('here');
. As written, your example method does not have the correct prototype to receive event observer data (missing an argument).
在这种情况下,将事件观察器用于重定向逻辑是完全合适的,正如核心团队将请求和响应对象显式传递给观察者所证明的那样.你的尝试很好,但我认为你有条件和配置导致执行流到 Mage_Checkout_CartController::_goBack()
,特别是行
Using an event observer for redirect logic is totally appropriate in this context, as evinced by the core team explicitly passing the request and response objects in to the observer. Your attempt is good, but I think that you have conditions and configuration which cause execution to flow to Mage_Checkout_CartController::_goBack()
, specifically to the line
$this->_redirect('checkout/cart');
所以我们需要修改我们的方法.现在,您可以通过操作响应并调用前端控制器的 sendResponse()
方法来阻止在事件观察者之后处理任何请求/响应逻辑,如下所示(注意:不要这样做!):
So we need to revise our approach. Now, you could prevent any request/response logic from processing after your event observer by manipulating the response and calling the Front Controller's sendResponse()
method as demonstrated below (nb: don't do this!):
public function moduleMethod($observer) //note I added a param
{
/* @var $response1 Mage_Core_Controller_Response_Http */
$response1 = $observer->getResponse(); // observers have event args
$url = 'http://www.example.com/';
$response1->setRedirect($url);
/* SHOULDN'T DO THIS */
Mage::app()->getFrontController()->sendResponse();
}
这应该可行,但我认为它通过触发虚幻系统组件 (EDA) 的输出来混淆关注的领域.让我们看看命令控制结构中是否有我们可以使用的东西...
This should work, but I think it mixes up areas of concern by triggering output from an ethereal system component (EDA). Let's see if there's something in the command-control structure which we can use...
就在 checkout_cart_add_product_complete
事件执行之后,进入购物车控制器的 _goBack() 方法.这个方法名称的问题在于它的作用比它的名字暗示的要多:
Just after the checkout_cart_add_product_complete
event execution comes to the cart controller's _goBack() method. The problem with this method name is that it does more than its name implies:
/**
* Set back redirect url to response
*
* @return Mage_Checkout_CartController
*/
protected function _goBack()
{
$returnUrl = $this->getRequest()->getParam('return_url');
if ($returnUrl) {
// clear layout messages in case of external url redirect
if ($this->_isUrlInternal($returnUrl)) {
$this->_getSession()->getMessages(true);
}
$this->getResponse()->setRedirect($returnUrl);
}
//...
}
看起来我们可以在请求对象上设置一个 return_url
参数并完成我们需要的.
It looks like we can just set a return_url
param on the request object and accomplish what we need.
public function moduleMethod(Varien_Event_Observer $observer)
{
$observer->getRequest()->setParam('return_url','http://www.google.com/');
}
我已经测试过了,它应该可以解决问题!
I've tested this, and it should do the trick!
这篇关于Magento - 从观察者方法重定向客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!