12

我对 magento 消息有疑问。我正在构建自定义模块,理论上应该能够限制对商店某些部分的访问。我创建了一个观察者,它挂钩 controller_action_predispatch事件并检查用户是否可以访问当前请求。如果无法访问该操作,则观察者重定向用户并设置错误信息。我想将重定向 url 设置为客户来自的页面,以避免点击整个商店。如果已设置,我正在查看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);

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

我在想问题出在完整的 url 和我的本地安装(我正在使用 .local 域)中,但我尝试添加

$url = str_replace(Mage::getBaseUrl(), '/', $url);

但这并没有帮助。

我也尝试使用 phpheader()函数进行重定向,但没有任何结果。

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

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

任何关于在哪里看的提示将不胜感激。

4

3 回答 3

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);

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

<script language=”javascript” type=”text/javascript”&gt;
window.location.href=”module/controller/action/getparam1/value1/etc";
</script>    
于 2013-02-21T10:03:48.037 回答
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;
    }
}

因此,您现在可以通过以下方式真正干净地实施您的解决方案:

/**
 * 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;
    }
}
于 2013-03-21T03:34:23.933 回答
0

this will work , so try it:

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

This means you are not allowing again to execute anything else.

于 2016-12-16T07:09:48.003 回答