Search code examples
phpeventsmagentomagento-1.5

Using Magento events with Mage::app('')


I'm trying to get a simple event observer working but I don't know if it is even possible under these circumstances. Ultimately, I'd like to fire an event when Shipworks pushes order updates. I think because Shipworks is done entirely in their shipworks3.php script and doesn't call Mage::run() to initialize the full store (it uses Mage::app(''), the observers aren't attached to the events...at least that's my going theory at this point. However, I can't seem to get it to work.

Below is some sample code I threw together to test this out. Please let me know if you have any thoughts on this

EXAMPLE

First, I created a simple module with an observer and a frontend controller for testing purposes:

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";
    }
}

Module Config XML

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

Next, to test this, I loaded http://my_domain/vpstest in my browser and I saw the expected output (i.e., it dispatched the event and my observer caught it)

I then created eventtest.php in the root of my site and hit that from my browser. In this case, Magento did fire the event, but my observer didn't catch it.

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";
}

So....what am I NOT doing? Is it even possible to use the Magento event system without the full Magento app running?

Thanks!


Solution

  • Typos will get you every time. In my config.xml, I had the <events>...</events> block outside the <global>...</global> block. Whoops... Fixing that fixed my problem.

    It's important to note that in this case, the global area is the only area loaded by default. So to attach my event observer to an event when you're just running Mage::app(''), you need to put it in the <global>...</global> section. Conversely, I believe WebFlakeStudio's answer should work, though I haven't tested it.

    Hope this helps someone.