For example, the Magento wiki has an example of customizing Magento using an observer. This example includes configuration of the module that provides the observer:
<?xml version="1.0"?> <config> <global> <models> <xyzcatalog> <class>Xyz_Catalog_Model</class> </xyzcatalog> </models> <events> <catalog_product_get_final_price> <observers> <xyz_catalog_price_observer> <type>singleton</type> <class>Xyz_Catalog_Model_Price_Observer</class> <method>apply_discount_percent</method> </xyz_catalog_price_observer> </observers> </catalog_product_get_final_price> </events> </global> </config>
I wondered why, in all the examples I found, the observer class was always a 'Model' class. It seems strange to me that a 'Model' (think MVC) would handle an event. I think of a Model as getting or persisting data. A controller seems a more appropriate component for handling an event. But, again, every example I have seen executes a method from a Model class. So, I had a look at the Magento code to see what was going on and whether there were any clues as to why a Model rather than a Controller.
In some cases, like the example above, the class name is given explicitly. In every case I have seen, the class name includes 'Model'. Note that in a case like this example, the Mage __autoload function changes '_' to directory separator so, on a Linux system, that class would be loaded from Xyz/Catalog/Model/Price/Observer.php. In this case, it is just a class and needn't be a Model as far as I can tell.
In other cases, the class is specified differently: as 'module/model'. In this case, the processing within Magento inserts 'Model' into the class name, so it is a bit more explicitly a Model class. See getGroupedClassName in Mage_Core_Model_Config for the full details. getGroupedClassName is called to transform the class name if it contains '/', in which case the class becomes getGroupedClassName('model', $class).
No comments:
Post a Comment