【问题标题】:Using Magento events with Mage::app('')将 Magento 事件与 Mage::app('') 一起使用
【发布时间】:2011-10-05 20:47:27
【问题描述】:

我正在尝试让一个简单的事件观察器工作,但我不知道在这种情况下是否可能。最后,我想在 Shipworks 推送订单更新时触发一个事件。我认为因为 Shipworks 完全是在他们的 shipworks3.php 脚本中完成的,并且没有调用 Mage::run() 来初始化完整的存储(它使用 Mage::app(''),观察者不依赖于事件......至少这是我的打算在这一点上的理论。但是,我似乎无法让它发挥作用。

下面是我拼凑起来测试的一些示例代码。如果您对此有任何想法,请告诉我

示例

首先,我创建了一个带有观察者和前端控制器的简单模块,用于测试目的:

config.xml

<config>
    <modules>
        <VPS_Test>
            <version>0.1.0</version>
        </VPS_Test>
    </modules>
    <global>
        <models>
            <vps_test>
                <class>VPS_Test_Model</class>
            </vps_test>
        </models>
    </global>
    <events>
        <test_event_one>
            <observers>
                <test_event_one>
                    <type>singleton</type>
                    <class>VPS_Test_Model_Observer</class>
                    <method>foo_test_global</method>
                </test_event_one>
            </observers>
        </test_event_one>
    </events>
    <frontend>
        <events>
            <test_event_one>
                <observers>
                    <test_event_one>
                        <type>singleton</type>
                        <class>VPS_Test_Model_Observer</class>
                        <method>foo_test_front</method>
                    </test_event_one>
                </observers>
            </test_event_one>
        </events>
        <routers>
            <vps_test>
                <use>standard</use>
                <args>
                    <module>VPS_Test</module>
                    <frontName>vpstest</frontName>
                </args>
            </vps_test>
        </routers>
    </frontend>
</config>

Observer.php

class VPS_Test_Model_Observer extends Mage_Core_Model_Abstract
{
    public function foo_test_front(Varien_Event_Observer $observer)
    {
        echo "foo_test event caught in observer FRONT";
    }


    public function foo_test_global(Varien_Event_Observer $observer)
    {
        echo "foo_test event caught in observer GLOBAL";
    }
}

模块配置 XML

<config>
    <modules>
        <VPS_Test>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
            </depends>
        </VPS_Test>
    </modules>
</config>

接下来,为了测试这一点,我在浏览器中加载了http://my_domain/vpstest,我看到了预期的输出(即,它调度了事件,而我的观察者捕获了它)

然后我在我的站点的根目录中创建了eventtest.php,并从我的浏览器中点击它。在这种情况下,Magento 确实触发了该事件,但我的观察者没有发现它。

eventtest.php

require 'app/Mage.php';
error_reporting(E_ALL | E_STRICT);
ini_set('html_errors', 1);
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);


try {
    Mage::app('');

    echo "event test<br/>";

    $foo = Mage::getModel('vps_test/observer');

    echo get_class($foo) . '<br />';

    Mage::dispatchEvent('test_event_one', array('object' => ''));
}
catch(Exception $e)
{
    echo "exception<br/>$e";
}

所以....我没有做什么?是否可以在不运行完整的 Magento 应用程序的情况下使用 Magento 事件系统?

谢谢!

【问题讨论】:

    标签: php events magento magento-1.5


    【解决方案1】:

    错字每次都会让你明白。在我的 config.xml 中,&lt;events&gt;...&lt;/events&gt; &lt;global&gt;...&lt;/global&gt; 块之外。哎呀...修复它解决了我的问题。

    需要注意的是,在这种情况下,global area 是默认加载的唯一区域。因此,当您刚刚运行Mage::app('') 时,要将我的事件观察器附加到一个事件,您需要将它放在&lt;global&gt;...&lt;/global&gt; 部分中。相反,我相信 WebFlakeStudio 的答案应该可以工作,尽管我还没有测试过。

    希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      我认为您需要包含以下几行才能使其正常工作:

      Mage::getConfig()->loadEventObservers($scope);
      Mage::app()->addEventArea($scope);
      

      其中$scope 的值可以是frontendadminhtml

      【讨论】:

        【解决方案3】:

        你不需要这个:

        <depends>
                    <Mage_Core />
        </depends>
        

        在您的模块配置中。

        【讨论】:

          猜你喜欢
          • 2012-12-26
          • 2013-09-17
          • 2014-02-04
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          • 1970-01-01
          • 2017-04-16
          • 2016-05-27
          相关资源
          最近更新 更多