Magento中的全局消息令人烦恼

时间:2014-12-04 10:05:47

标签: php magento messages

我在magento中的知识不是那么深,所以我在magento全局消息输出方面遇到了麻烦。

我创建了我的模块。在我的模块的索引页面(路线是/优惠券)我有一个表格。所以通过POST我发送数据到Controller(路由是相同的(/ voucehrs))我验证数据:

if(!isset($post["to_name"]) || !$post['to_name'])
   {
       Mage::getSingleton('customer/session')->addError($this->__('Please enter  "To Name" information'));
       $errors = 1;
   }

...

  if(!$errors)
     {
        Mage::getSingleton('customer/session')->addSuccess($this->__('Your order is successfully saved and now is in process'));
     }

在模板中我使用<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>但没有任何反应,尽管我将Session变量转储到可以看到成功消息的位置。

我哪里错了?

5 个答案:

答案 0 :(得分:3)

请检查您的控制器。如果渲染布局,则必须在操作中包含以下代码。

$this->_initLayoutMessages('customer/session');

所以你的行动应该是这样的:

$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->renderLayout();

它可能对你有帮助。

答案 1 :(得分:2)

Notice
Mage::getSingleton('core/session')->addNotice('Notice message');

enter image description here

Success
Mage::getSingleton('core/session')->addSuccess('Success message');

enter image description here

Error
Mage::getSingleton('core/session')->addError('Error message');

enter image description here

Warning (admin only)
Mage::getSingleton('adminhtml/session')->addWarning('Warning message'); 

enter image description here

来自控制器的输出错误

public function sendAction()
    {
        try
        {
            $mail = new Zend_Mail();
            $mail->send();
        }
        catch (Exception $e)
        {
            Mage::getSingleton('core/session')->addError('Error sending mail');
        }
        $this->loadLayout();
        $this->renderLayout();
    }

MOre info for session in mageno how to get and set

答案 2 :(得分:0)

您可以尝试core/session代替customer/session吗?它可能对你有帮助。

答案 3 :(得分:0)

首先,我们强烈建议您安装/配置Xdebug - 此工具可为您节省大量时间。

至于问题,请尝试使用

Mage::getSingleton('customer/session')

而不是

Mage::getSingleton('core/session')

如果不起作用,您可以尝试:

// after post form
$this->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton(‘customer/session’)->getMessages(true));

// when display
$this->getLayout()->getMessagesBlock()->getGroupedHtml();

希望它会有所帮助。 :)

答案 4 :(得分:0)

我发现这个堆栈溢出非常有帮助。

对我有用的是Alex Ivanov和Emipro Technologies Pvt的组合。有限公司的回答者,如下(控制器代码):

$this->loadLayout();
$this->_initLayoutMessages('core/session');
$this->renderLayout();