Magento在重定向后丢失消息

Magento在重定向后丢失消息,magento,Magento,我对magento邮件有问题。我正在构建自定义模块,理论上应该能够限制对商店某些部分的访问。我已经创建了一个观察器,它钩住controller\u action\u predispatch事件,检查用户是否可以访问当前请求。如果无法访问该操作,观察员将重定向用户并设置错误信息。我想将重定向url设置为客户来自的页面,以避免点击整个店铺。我正在查看HTTP\u REFERER,如果设置了,就使用它,否则我会将客户重定向到主页。问题是,在后一种情况下(主页重定向),一切都很好,但当我根据refere

我对magento邮件有问题。我正在构建自定义模块,理论上应该能够限制对商店某些部分的访问。我已经创建了一个观察器,它钩住
controller\u action\u predispatch
事件,检查用户是否可以访问当前请求。如果无法访问该操作,观察员将重定向用户并设置错误信息。我想将重定向url设置为客户来自的页面,以避免点击整个店铺。我正在查看
HTTP\u REFERER
,如果设置了,就使用它,否则我会将客户重定向到主页。问题是,在后一种情况下(主页重定向),一切都很好,但当我根据referer设置url时,在消息框中看不到错误消息

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

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

我认为问题在于完整的url和我的本地安装(我使用的是.local域),但我尝试添加

$url = str_replace(Mage::getBaseUrl(), '/', $url);
但这没有帮助

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

所有缓存都已禁用。引发问题的工作流如下所示:

  • 我将转到任何可访问的页面(例如/customer/account)
  • 单击购物车链接(此帐户的购物车已禁用)
  • 返回到/customer/account并显示错误消息
  • 再次点击购物车链接
  • 返回到/customer/account,但没有错误消息
  • 任何关于在哪里寻找的提示都将不胜感激

    //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);
    
    这将在控制器中工作,但如果您试图在输出已发送后重定向,则只能通过javascript执行:

    <script language=”javascript” type=”text/javascript”>
    window.location.href=”module/controller/action/getparam1/value1/etc";
    </script>    
    
    
    window.location.href=“module/controller/action/getparam1/value1/etc”;
    
    您的消息会丢失,因为您在
    控制器\u操作\u predispatch
    中使用了不利于重定向的方式。您的解决方案一方面会导致“消息丢失”,另一方面会浪费服务器的处理能力

    当您查看
    Mage_Core_Controller_Varien_Action::dispatch()时
    ,您将看到您的解决方案不会停止当前操作的执行,但它应该通过重定向来停止。相反,Magento会将当前操作执行到底,包括呈现您之前添加的消息。因此,难怪消息会在下一个客户端请求中丢失,Magento之前就已经呈现了或者,使用包含重定向的服务器响应

    此外,您将在
    Mage\u Core\u Controller\u Varien\u Action::dispatch()
    中看到,只有一种可能停止当前操作的执行并直接跳到重定向,即第428行
    catch(Mage\u Core\u Controller\u Varien\u Exception$e)[……]
    。因此,您必须使用
    Mage\u Core\u Controller\u Varien\u Exception
    ,这是非常不受欢迎的,但唯一适合您的解决方案。唯一的问题是,自从Magento 1.3.2引入该类以来,该类存在一个bug。但这很容易修复

    只需创建您自己的类,该类派生自
    Mage\u Core\u Controller\u Varien\u 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;
        }
    }
    
    因此,您现在可以通过以下方式实现真正干净的解决方案:

    /**
     * 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;
        }
    }
    

    这将起作用,因此请尝试:

    $url = 'path/to/your/page';
    $this->_redirectUrl($url);
    return false;
    

    这意味着您不允许再次执行任何其他操作。

    您是否可以停用所有缓存(+FPC如果magento EE)以查看是否存在缓存问题?顺便说一下,我不理解您的所有说明,您知道会话错误消息在第一次显示后会被删除?我扩展了说明,希望现在清楚。
    $url = 'path/to/your/page';
    $this->_redirectUrl($url);
    return false;