Magento重定向后丢失消息

Zef*_*ryn 12 magento

我有magento消息的问题.我正在构建自定义模块,理论上应该能够限制对商店某些部分的访问.我创建了一个挂钩controller_action_predispatch事件的观察者, 并检查用户是否可以访问当前请求.如果无法访问操作,观察者会重定向用户并设置错误信息.我想将重定向网址设置为客户来自的网页,以避免点击整个商店.我正在查看HTTP_REFERER并使用它,如果它已设置,否则我将客户重定向到主页.问题是在后一种情况下(主页重定向)一切都很好但是当我根据引用设置url时,我没有在消息框中看到错误消息.

来自观察者的代码($name变量是一个字符串):

Mage::getSingleton('core/session')->addError('Acces to '.$name.' section is denied');
$url = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer()  : Mage::getUrl();
Mage::app()->getResponse()->setRedirect($url);
Run Code Online (Sandbox Code Playgroud)

我发现有趣的是,如果我在观察者文件中进行任何更改并保存它,那么下一个失败并被重定向到referer url的请求会显示错误信息,但随后会丢失消息.

我在想这个问题是在完整的URL和我的本地安装(我正在使用.local域),但所以我尝试添加

$url = str_replace(Mage::getBaseUrl(), '/', $url);
Run Code Online (Sandbox Code Playgroud)

但这没有帮助.

我也尝试使用PHP header()函数重定向,没有任何结果.

所有缓存都被禁用.触发问题的工作流程如下:

  1. 我要去任何可访问的页面(例如/ customer/account)
  2. 点击购物车链接(此帐户的购物车已停用)
  3. 返回/ customer/account并显示错误消息
  4. 再次点击购物车链接
  5. 返回/ customer/account但没有错误消息

任何关于在哪里看的提示都将受到赞赏.

Sil*_*mer 24

//A Success Message
Mage::getSingleton('core/session')->addSuccess("Some success message");

//A Error Message
Mage::getSingleton('core/session')->addError("Some error message");

//A Info Message (See link below)
Mage::getSingleton('core/session')->addNotice("This is just a FYI message...");

//These lines are required to get it to work
session_write_close(); //THIS LINE IS VERY IMPORTANT!
$this->_redirect('module/controller/action');

// or
$url = 'path/to/your/page';
$this->_redirectUrl($url);
Run Code Online (Sandbox Code Playgroud)

这将在控制器中工作,但如果您在输出已经发送后尝试重定向,那么您只能通过javascript执行此操作:

<script language=”javascript” type=”text/javascript”>
window.location.href=”module/controller/action/getparam1/value1/etc";
</script>    
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,session_write_close(); 为我做了诀窍. (7认同)

wit*_*rin 4

您的消息会丢失,因为您在controller_action_predispatch. 您的解决方案一方面会导致“消息丢失”,另一方面会浪费服务器的处理能力。

当您查看 时Mage_Core_Controller_Varien_Action::dispatch(),您会发现您的解决方案不会停止当前操作的执行,但它应该通过重定向来执行此操作。相反,Magento 会执行当前操作直至结束,包括渲染您之前添加的消息。因此,难怪为什么消息会随着下一个客户端请求而丢失,Magento 之前已经渲染过该消息,并且服务器响应包含您的重定向。

此外,您将看到Mage_Core_Controller_Varien_Action::dispatch()只有一种可能停止当前操作的执行并直接跳到第 428 行中的重定向catch (Mage_Core_Controller_Varien_Exception $e) [...]。因此,您必须使用Mage_Core_Controller_Varien_Exception非常不受欢迎的解决方案,但这是适合您的目的的唯一正确的解决方案。唯一的问题是,这个类自 Magento 1.3.2 中引入以来有一个错误。但这很容易解决。

只需创建您自己的类,该类派生自Mage_Core_Controller_Varien_Exception

/**
 * Controller exception that can fork different actions, 
 * cause forward or redirect
 */
class Your_Module_Controller_Varien_Exception 
    extends Mage_Core_Controller_Varien_Exception
{
    /**
     * Bugfix
     * 
     * @see Mage_Core_Controller_Varien_Exception::prepareRedirect()
     */
    public function prepareRedirect($path, $arguments = array())
    {
        $this->_resultCallback = self::RESULT_REDIRECT;
        $this->_resultCallbackParams = array($path, $arguments);
        return $this;
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,您现在可以使用以下方法真正干净地实施您的解决方案:

/**
 * Your observer
 */
class Your_Module_Model_Observer
{
    /**
     * Called before frontend action dispatch
     * (controller_action_predispatch)
     * 
     * @param Varien_Event_Observer $observer
     */
    public function onFrontendActionDispatch($observer)
    {
        // [...]

        /* @var $action Mage_Core_Model_Session */
        $session = Mage::getSingleton('core/session');
        /* @var $helper Mage_Core_Helper_Http */
        $helper = Mage::helper('core/http');
        // puts your message in the session
        $session->addError('Your message');
        // prepares the redirect url
        $params = array();
        $params['_direct'] = $helper->getHttpReferer() 
            ? $helper->getHttpReferer() : Mage::getHomeUrl();
        // force the redirect
        $exception = new Your_Module_Controller_Varien_Exception();
        $exception->prepareRedirect('', $params);
        throw $exception;
    }
}
Run Code Online (Sandbox Code Playgroud)