且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Magento中建立观察员的正确方法是什么?

更新时间:2023-11-29 22:20:04

我在任何地方都看不到事件名称,但是我将在此处发布一般情况:

I don't see the event name anywhere, but I'll post the general case here:

假设:您已经设置了一个模块,并且已从Yourmodule/Model目录正确加载了模型.

Assumed: That you have a module set up, with models being loaded correctly from the Yourmodule/Model directory..

在模块的config.xml文件中:

In your module's config.xml file:

<config>
    <global>
  <events>
   <full_event_name>
    <observers>
     <yourmodule>
      <type>singleton</type>
      <class>yourmodule/observer</class>
      <method>yourMethodName</method>
     </yourmodule>
    </observers>
   </full_event_name>
  </events>
 </global>
</config>

使用以下内容创建文件%yourmodule%/Model/Observer.php:

Create a file %yourmodule%/Model/Observer.php with the following contents:

<?php

class Yourmodule_Model_Observer {

    public function yourMethodName($event) {
        $data = $event->getData(); // this follows normal Magento data access

        // perform your action here
    }

}//class Yourmodule_Model_Observer

确实,您可以在观察者中随意命名该方法,但是这种模式似乎是将类本身命名为Observer.使用常规模型加载来加载它(例如yourmodule/observer映射到Yourmodule_Model_Observer).希望有帮助!

Really, you can name the method whatever you want within your observer, but the pattern seems to be to name the class itself Observer. It is loaded using normal model loading (e.g. yourmodule/observer maps to Yourmodule_Model_Observer). Hope that helps!

谢谢, 乔