在Magento中没有获得成功消息

时间:2014-07-08 09:36:43

标签: php magento

我有一个名为IndexController.php的控制器。当我删除deleteAction()中的记录时,我收到消息。但是当我在addcategoryAction中添加或编辑记录时,我没有收到任何消息。任何人都可以在我的控制器中查看并检查为什么我没有在addcategoryAction中收到任何消息。这是我的控制器。

<?php
class Company_Web_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {    
        $this->loadLayout();     
        $this->renderLayout();
    }

    public function addcategoryAction()
    {
        if ($this->getRequest()->isPost()) 
        {
            $data = $this->getRequest()->getParams();
            $id = $data['editid'];
            $catName = $data['catName'];
            $status = $data['status'];
            $data = array('name'=>$catName,'status'=>$status);
            $model = Mage::getModel('web/web');
            if($id == '')
            {
                $model->setData($data);
                try {
                        $insertId = $model->save()->getId();
                } catch (Exception $e){
                 echo $e->getMessage();
                }
                Mage::getSingleton('core/session')->addSuccess($this->__('Category has been successfully Added'));
            }
            else
            {
                $model->load($id)->addData($data);
                try {
                        $model->setId($id)->save();
                } catch (Exception $e){
                    echo $e->getMessage();
                }
                Mage::getSingleton('core/session')->addSuccess($this->__('Category has been successfully Updated'));
            }
            $this->_redirect('web/index/category');

        }
        $this->loadLayout();     
        $this->renderLayout();
    }

    public function categoryAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

    public function deleteAction()
    {
        $id =  $this->getRequest()->getParam('id');
        $model = Mage::getModel('web/web');
        $model->setId($id)->delete();
        Mage::getSingleton('core/session')->addSuccess($this->__('Category has been successfully Deleted'));
        $this->_redirect('web/index/category');
    }
}

?>

1 个答案:

答案 0 :(得分:2)

在控制器操作中添加以下代码

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

在使用下面的代码

渲染布局之前
$this->renderLayout();

以及您在phtml文件<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>中的以下代码 根据{{​​1}},它会在模型​​保存后重定向到addcategoryAction

SO.modifed categoryAction是

categoryAction

public function categoryAction() { $this->loadLayout(); $this->_initLayoutMessages('customer/session'); $this->_initLayoutMessages('core/session'); $this->renderLayout(); }

的phtml文件中添加&lt; div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>