Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用前端将Magento 1.9 admin中的全局消息显示为最新消息_Php_Forms_Magento_Magento 1.9 - Fatal编程技术网

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

Php 如何使用前端将Magento 1.9 admin中的全局消息显示为最新消息,php,forms,magento,magento-1.9,Php,Forms,Magento,Magento 1.9,如何使用前端将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"]))

如何使用前端将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??
}
?>


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

您将不需要在
通知
区域挂接一些东西

注意:在下文中,我只涉及使消息显示在全局通知区域中所需的文件和部分。这还不足以进行完整的扩展。请在别处查找该信息。好的开始是

您需要在扩展布局文件中使用块引用
通知
区域

文件结构 以下是我们将要介绍的文件:

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 与其他内容一样,它从您的
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>
块零件类型是允许Magento查找块的字符串

<block type="yourextension/adminhtml_notifications" name="notifications_yourcompany" />
模型:app/code/local/Yourcompany/Youextension/Model/Notification.php 这个模型对我们来说非常简单。将
通知
模型看作是消息的容器,而不是单个通知

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
中设置它,以便在
之前侦听
您的扩展通知。因此,当我们的块即将呈现时,我们可以选择首先向通知模型添加消息

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
以侦听某个事件,即我们将要呈现通知的事件

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;
    }
}
我们已经创建了一个参考Magento自己的通知区域的布局,在所有页面上,我们将呈现自己的块

由于
Model\Notification
是一个单例,因此我们可以
addMessage()
s从扩展(和外部)的所有部分添加到它,当我们调用
getMessages()
时,我们将获得所有消息。我们不必担心临时存储多条消息

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


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

您将不需要在
通知
区域挂接一些东西

注意:在下文中,我只涉及使消息显示在全局通知区域中所需的文件和部分。这还不足以进行完整的扩展。请在别处查找该信息。好的开始是

您需要在扩展布局文件中使用块引用
通知
区域

文件结构 以下是我们将要介绍的文件:

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 与其他内容一样,它从您的
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>
块零件类型是允许Magento查找块的字符串

<block type="yourextension/adminhtml_notifications" name="notifications_yourcompany" />
模型:app/code/local/Yourcompany/Youextension/Model/Notification.php 这个模型对我们来说非常简单。将
通知
模型看作是消息的容器,而不是单个通知

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
中设置它,以便在
之前侦听
您的扩展通知。因此,当我们的块即将呈现时,我们可以选择首先向通知模型添加消息

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
以侦听某个事件,即我们将要呈现通知的事件

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;
    }
}
我们已经创建了一个参考Magento自己的通知区域的布局,在所有页面上,我们将呈现自己的块

由于
Model\Notification
是一个单例,因此我们可以
addMessage()
s从扩展(和外部)的所有部分添加到它,当我们调用
getMessages()
时,我们将获得所有消息。我们不必担心临时存储多条消息

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

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