dsfsda121545
dsfsda121545
2014-05-18 08:46

如何显示来自控制器的信息以便在magento中查看?

已采纳

I will like to show message from controller to view in magento. In controller I have

    $arrError=Mage::getModel('advert/advert')->isValid(array('step1','step2'));
        if (!empty($arrError)) {
            $strReturnPath = $arrError['return_path'];
            unset($arrError['return_path']);
            foreach ($arrError as $strError) {
                Mage::getSingleton('customer/session')->addError( $strError );
                //var_dump($strError);

            }

            $this->_redirect($strReturnPath);
        }

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

        $this->renderLayout();

And in view I have

<div id="messages_product_error_view">
    <?php

        Mage::app()->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton('customer/session')->getMessages(true));
        echo  Mage::app()->getLayout()->getMessagesBlock()->toHtml();
    ?>
</div>

In controller when I do a var_dump I can see the error messages. With redirect, the page does redirect to the view page. However, in the div I expect the message to be shown. ANd it is not showing Please please please help?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongqi1245 dongqi1245 7年前

    I think _redirect() method in controller does not stop script execution. It only sets up redirect headers. Because of that it still process loading layout, initializing layout messages and rendering it. This will make magento read messages in the same process and clear them before the actual redirect. Try adding this code after calling _redirect() method

    $this->getResponse()->sendResponse();
    

    or rearrange your code in this manner:

    if (!empty($arrError)) {
            $strReturnPath = $arrError['return_path'];
            unset($arrError['return_path']);
            foreach ($arrError as $strError) {
                Mage::getSingleton('customer/session')->addError( $strError );
                //var_dump($strError);
    
            }
    
            $this->_redirect($strReturnPath);
        }
     else {
        $this->loadLayout();
        $this->_initLayoutMessages('customer/session');
    
        $this->renderLayout();
     }
    
    点赞 评论 复制链接分享
  • dongritan5654 dongritan5654 7年前
    Mage::app()->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton('customer/session')->getMessages(true));
    

    was resetting my session . As a result, I was lossing the messages. Crap! Damn! Bugger!

    Anyway, by removing the line I have the solution working.

    点赞 评论 复制链接分享
  • douqie1852 douqie1852 7年前

    When you debug Mage::getSingleton('customer/session')->getMessages(true) in your view, does it contain any of the messages?

    If you want to understand Magento notification better, you may find more about it here.

    点赞 评论 复制链接分享

相关推荐