Magento: How do I get observers to work in an external script?

前端 未结 1 809
北恋
北恋 2021-02-03 11:24

As far as I can tell, when a script is run outside of Magento, observers are not invoked when an event is fired. Why? How do I fix it?

Below is the original issue th

相关标签:
1条回答
  • 2021-02-03 11:56

    My first guess would be the event you're trying to hook into is a <frontend /> or <admin /> event, because it looks like only <global /> events fire when you run a command line script.

    Magento has this concept called "areas". Areas are sort-of like individual applications that live in the system (but not quite, I'm still a little fuzzy on the concept).When you setup a config.xml with your observers, you're either placing them in a <global /> tag, a <frontend /> tag, or a <admin /> tag.

    When it comes to events, Magento only loads up areas that it has to deal with for a particualr request. So, the <global /> area always gets loaded. However, the <frontend /> or <admin /> areas only get loaded up if the application gets to the controller dispatch. Specifcally, in the following file/line

    File: app/code/core/Mage/Core/Controller/Varien/Action.php
    Mage::app()->loadArea($this->getLayout()->getArea());
    

    That never happens with a command line application. Only the <global /> area gets loaded.

    So, as mentioned in the first paragraph, my guess is your observer isn't firing because Magento never loads the <frontend /> area of the application. As for solutions, you could try moving your observer to the <global /> area. You could also try manually calling

    Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);
    

    although, you'd be loading ALL observers in the <frontend /> area, many of which have probably been created assuming a web browser context.

    If none of that helps, take a look at the dispatchEvent method on the Mage_Core_Model_App class. That's where event observers get called.

    0 讨论(0)
提交回复
热议问题