如何使用前端将Magento 1.9管理员中的全局消息显示为最新消息


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

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

<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??
}
?>

当我单击提交时,它必须发送一条消息并在管理面板上显示全局消息区域。

你不会有一些东西可以钩到notifications区域。

注意:在下文中,我只涉及使消息出现在全局通知区域中所需的文件和部分。这还不足以进行完整的扩展。在其他地方寻找这些信息。一个好的开始是丝绸模块创建者

您需要在扩展布局文件中引用带有块notifications区域。

文件结构

这些是我们将在下面介绍的文件:

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

配置:app/code/local/Yourcompany/Youextension/etc/config.xml

与其他一切一样,它始于您的config.xml .您可能已经为扩展定义了块和模型部分,但我已将它们包含在此处以完成。

需要注意的重要部分是对布局文件的引用,以及我们将设置的用于侦听消息的观察器:

<?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>

布局:design/adminhtml/default/default/layout/yourcompany/yourextension.xml

在布局文件中,您必须引用通知区域。它简称为notifications

<default>是使用此布局的路径。 <default>意味着无处不在。

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

部分类型是字符串,允许Magento查找块。

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

第一部分显然yourextension告诉它查看您的扩展路径:app/code/local/Yourcompany/Youextension

第二部分adminhtml_notifications变成:Adminhtml/Notifications.php

这两个部分被Block和你的有,中提琴:app/code/local/Yourcompany/Youextension/Block/Adminhtml/Notifications.php粘在一起

<block name="yourextension"/>中的名称必须是唯一的。

app/code/local/yourcompany/youextension/block/Notifications.php

在此示例中,块将获取数据并直接呈现 HTML。通常,您还会包含一个模板,但为此没有必要。

为了获取消息,我们使用观察服务器模式。这意味着我们发送一条消息,即将写出通知。这意味着扩展程序的其他部分甚至其他扩展程序都可以选择添加消息。

这是下面的Mage::dispatchEvent('yourextension_notifications_before')部分。

如果扩展的另一部分侦听此事件并将消息添加到我们的通知模型,那么我们很幸运。事实上,我们已经从config.xml中知道,我们的Observer模型将监听此事件。

因此,当我们在Notification模型上调用getMessages()时,下面的消息将神奇地出现。

_toHtml的最后一部分是呈现通知的内容。

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;
    }
}
模型

:应用程序/代码/本地/您的公司/您扩展/模型/通知.php

该模型对于我们的目的来说非常简单。将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;
    }
}

观察者(发送者):应用/代码/本地/您的公司/您扩展/模型/观察者.php

观察者是魔法的最后一块。我们在config.xml中设置它以收听yourextension_notifications_before 。因此,当我们的块即将渲染时,我们可以选择先向通知模型添加消息。

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

因此,一旦扩展启动,它就会注册Model/Observer侦听某个事件,即我们将要呈现通知的事件。

我们创建了一个引用Magento自己的通知区域的布局,并且在所有页面上我们将渲染自己的块。

由于Model'Notification是一个单例,我们可以从我们的分机(和外部)的各个部分addMessage()它,当我们打电话给getMessages()时,我们会得到它们。我们不必担心暂时存储多条消息。

就像我开始这个答案一样,我假设您已经在使用您包含的表单的页面上工作。您还应该让它将消息设置为Notification模型。

您希望如何存储消息 - 在会话中或使用模型资源保存在数据库中。