从观察者重定向的Magento [英] Magento Redirect from Observer

查看:72
本文介绍了从观察者重定向的Magento的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难从观察者那里在Magento中创建一个有效的重定向.除此之外,我需要理解为什么像在控制器中一样,异常无法在观察者中起作用.

I am having trouble to create a working redirect in Magento from an observer.Apart from that I need to understand why the exception just like we do in controller does not work in Observer.

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

The typical exception done in controller is like below (adminhtml controller)

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

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

Somewhere in the blog I read about the below method to redirect from observer.

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

与观察者和控制器一起使用时,我不了解基本的重定向差异.

I don't understand the basic redirection difference when doing with an observer and controller.

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

Why controller redirection does not work when used in observer.

请帮我解释一下.

非常感谢.

推荐答案

请参见以下链接,我也发布了代码 它可能会帮助您.

See below link and i also have posted code it might help you.

来源: http://aaronbonner.io/post/418012530/redirects-内部控制器

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

Redirects in Magento Controllers In Zend Framework controllers, to output something other than html you will want to disable ViewHelper defaults along with some of the other magic ZF typically weaves.

在Magento中,执行控制器重定向时同样适用.

In Magento, the same thing applies when doing a controller redirect.

我注意到在PHP 5.2上,重定向似乎被忽略了,而我的Macports 5.3设置却起作用了.原来我错过了一个把戏,因此在Magento中进行了正确的重定向,如下所示:

I noticed on PHP 5.2 a redirect seemed to be ignored whereas my Macports 5.3 setup it worked. Turns out I was missing a trick, and so a proper redirect in Magento is done as follows:

MyPackage/MyModule/controllers/MyController.php中:

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

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

Without the setFlag call, your redirect will be ignored. The return call stops any further code execution and sees your redirect get actioned.

这篇关于从观察者重定向的Magento的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆