ds11111111111111111 2016-01-29 12:25
浏览 57
已采纳

如何使用前端在Magento 1.9 admin中显示全局消息作为最新消息

How to show global messages in Magento 1.9 admin as latest message by using front end.

<form action = "index.php" method = "post">
<input type = "text" name = "mymsg" />
<input type = "submit" name = "submit" value = "submit" />
</form>

<?php
if(isset($_POST["submit"]))
{
 ??code??
}
?>

When I click on submit it has to send a message and show on admin panels global message area.

  • 写回答

1条回答 默认 最新

  • dpwjx32578146 2016-01-29 13:49
    关注

    You will not a few things to hook into the notifications area.

    Note: in the following I'm only touching on the files and parts needed to make messages appear in the global notification area. This is not enough to make a complete extension. Seek that information elsewhere. A good start is Silk Module Creator

    You'll need reference notifications area with a block in your extensions layout file.

    File structure

    These are the files we are going to cover below:

    app/code/local/Yourcompany/Youextension/Block/Adminhtml/Notifications.php     
    app/code/local/Yourcompany/Youextension/etc/config.xml
    app/code/local/Yourcompany/Youextension/Model/Notification.php
    app/code/local/Yourcompany/Youextension/Model/Observer.php
    design/adminhtml/default/default/layout/yourcompany/yourextension.xml
    

    Config: app/code/local/Yourcompany/Youextension/etc/config.xml

    As everything else it starts in your config.xml. You'll probably already have block- and model-section defined for your extension, but I've included them here for completion.

    The important part to notice is the reference to the layout file and the observer we'll setup to listen for messages:

    <?xml version="1.0"?>
    <config>
        <modules>
            <Yourcompany_Yourextension>
                <version>0.1.0</version>
            </Yourcompany_Yourextension>
        </modules>
        <global>
            ...
            <blocks>
                <yourextension>
                    <class>Yourcompany_Yourextension_Block</class>
                </yourextension>
            </blocks>
            <models>
                <yourextension>
                    <class>Yourcompany_Yourextension_Model</class>
                </yourextension>
            </models>
            <events>
                <yourextension_notifications_before>
                    <observers>
                        <yourextension_observer>
                            <type>singleton</type>
                            <class>Yourcompany_Yourextension_Model_Observer</class>
                            <method>checkMessages</method>
                        </yourextension_observer>
                    </observers>
                </yourextension_notifications_before>
            </events>
    
            ...
        <adminhtml>
            ...
            <layout>
                <updates>
                    <yourextension>
                        <file>yourcompany/yourextension.xml</file>
                    </yourextension>
                </updates>
            </layout>
            ...
        </adminhtml>
    </config>
    

    Layout: design/adminhtml/default/default/layout/yourcompany/yourextension.xml

    In the layout file you'll have to make a reference to the notification area. It is simply called notifications.

    The <default> is the path for which this layout is to be used. <default> means everywhere.

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <default>
            <reference name="notifications">
                <block type="yourextension/adminhtml_notifications" name="notifications_yourcompany" />
            </reference>
        </default>
    </layout>
    

    The block part type is string that lets Magento look up the block.

    <block type="yourextension/adminhtml_notifications" name="notifications_yourcompany" />
    

    The first part yourextension obviously tells it to look in your extension path: app/code/local/Yourcompany/Youextension

    The second part adminhtml_notifications turns into: Adminhtml/Notifications.php

    These two parts are glued together by Block and your have, viola: app/code/local/Yourcompany/Youextension/Block/Adminhtml/Notifications.php

    The name in <block name="yourextension"/> just have to be unique.

    app/code/local/Yourcompany/Youextension/Block/Notifications.php

    In this example the block will get the data and render the HTML directly. Normally you would include a template as well, but for this it is not necessary.

    To get the messages we use an oberserver-pattern. This means that we send out a message that we about to write out the notifications. This means that other parts of the extension or even other extensions can opt to add messages.

    This is Mage::dispatchEvent('yourextension_notifications_before') part below.

    If another part of the extension listens to this event and adds messages to our Notification model, then we are in luck. In fact we already know from config.xml that our Observer model will listen to this event.

    So when we call getMessages() on our Notification model below messages will have magically appeared.

    The last part of the _toHtml is what renders the notifications.

    class Yourcompany_Yourextension_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template
    {
        public function _toHtml($className = "notification-global")
        {
            // Let other extensions add messages
            Mage::dispatchEvent('yourextension_notifications_before');
            // Get the global notification object
            $messages = Mage::getSingleton('yourextension/notification')->getMessages();
            $html = null;
            foreach ($messages as $message) {
                $html .= "<div class='$className'>" . $message . "</div>";
            }
            return $html;
        }
    }
    

    Model: app/code/local/Yourcompany/Youextension/Model/Notification.php

    The model is super simple for our purpose. Think of the Notification model as a container for messages rather than a single notification.

    class Yourcompany_Yourextension_Model_Notification extends Varien_object
    {
        protected $messages = [ ];
    
        public function getMessages()
        {
            return $this->messages;
        }
    
        public function setMessages($messages)
        {
            $this->messages = $messages;
            return $this;
        }
    
        public function addMessage($message)
        {
            $this->messages[] = $message;
            return $this;
        }
    }
    

    Observer (sender): app/code/local/Yourcompany/Youextension/Model/Observer.php

    The observer is the last piece of the magic. We set it up in config.xml to listen for yourextension_notifications_before. So when our block is about to render we have the option to add a message to the Notification model first.

    class Yourcompany_Yourextension_Model_Observer
    {
    
        public function checkMessages($observer)
        {
            $notifications = Mage::getSingleton('yourextension/notification');
            $notifications->addMessage("I was sent by Yourextension");
            return $observer;
        }
    }
    

    Wrap up

    So once you extension boots up it registers Model/Observer to listen for a certain event—the event that we are about to render notifications.

    We have created a layout that references Magento's own notification area and on all pages we will render our own block.

    The since Model\Notification is a singleton we can addMessage()s to it from all over our extension (and outside) and when we call getMessages() we'll get them all. We don't have to worry about storing multiple messages temporarily.

    Like I started this answer, I'm assuming you area already working on a page with the form you included. You should have it set messages to Notification model as well.

    It is up to you how you want to store the messages—in session or using a model resource to save in the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘