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

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区。

注意:下面我只涉及使消息出现在全局通知区域所需的文件和部分。这不足以进行完整的扩展。在别处寻找该信息。好的开始是 Silk Module Creator

您需要在扩展 布局文件.

中参考 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;
    }
}

型号:app/code/local/Yourcompany/Youextension/Model/Notification.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;
    }
}

观察者(发件人):app/code/local/Yourcompany/Youextension/Model/Observer.php

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

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 模型。

如何存储消息取决于您——在会话中或使用模型资源保存在数据库中。