Magento Observer 没有做太多观察
这是我的第一个模块,所以我认为会有错误。但我陷入困境,并认为堆栈溢出社区的情报可以提供帮助。
本质上,我希望我的模块侦听目录搜索索引更新的事件,并基于该事件执行一些代码。
所以我告诉magento识别我的模块:
app/etc/modules/Nate_SearchToFind.xml
<?xml version="1.0"?>
<config>
<modules>
<Nate_SearchToFind>
<active>true</active>
<codePool>local</codePool>
</Nate_SearchToFind>
</modules>
</config>
然后在: app/local/Nate/SearchToFind/etc/config.xml
<?xml version="1.0"?>
<config>
<global>
<models>
<natesearchtofindbundle>
<class>Nate_SearchToFind_Bundle_Model</class>
</natesearchtofindbundle>
</models>
<events>
<catalogindex_plain_reindex_after>
<observers>
<Nate_SearchToFind_Observer>
<type>singleton</type>
<class>Nate_SearchToFind_Bundle_Model_Observer</class>
<method>beautify_search</method>
</Nate_SearchToFind_Observer>
</observers>
</catalogindex_plain_reindex_after>
</events>
</global>
</config>
然后位于:app/code/local/Nate/SearchToFind/Model/Observer.php
<?php
class Nate_SearchToFind_Bundle_Model_Observer
{
public function __construct()
{
}
public function beautify_search($observer)
{
//perform function operations here
}
}
有没有人发现我的代码中的一些错误(我确信它们在那里)或作为我的整体方法,但我似乎找不到他们...感谢您的帮助!
So this is my first module, so I figured there would be errors. But I am stuck and thought the intelligence of the stack overflow community could help out.
Essentially I want my module to listen for the event of a catalog search index update and perform some code based on that.
So I told magento to recognize my module in:
app/etc/modules/Nate_SearchToFind.xml
<?xml version="1.0"?>
<config>
<modules>
<Nate_SearchToFind>
<active>true</active>
<codePool>local</codePool>
</Nate_SearchToFind>
</modules>
</config>
Then in: app/local/Nate/SearchToFind/etc/config.xml
<?xml version="1.0"?>
<config>
<global>
<models>
<natesearchtofindbundle>
<class>Nate_SearchToFind_Bundle_Model</class>
</natesearchtofindbundle>
</models>
<events>
<catalogindex_plain_reindex_after>
<observers>
<Nate_SearchToFind_Observer>
<type>singleton</type>
<class>Nate_SearchToFind_Bundle_Model_Observer</class>
<method>beautify_search</method>
</Nate_SearchToFind_Observer>
</observers>
</catalogindex_plain_reindex_after>
</events>
</global>
</config>
Then in: app/code/local/Nate/SearchToFind/Model/Observer.php
<?php
class Nate_SearchToFind_Bundle_Model_Observer
{
public function __construct()
{
}
public function beautify_search($observer)
{
//perform function operations here
}
}
Does anyone spot some errors in my code (I'm sure they are in there) or as my approach as a whole, but I cannot seem to find them...Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的观察者类名称错误。它应该是 PHP 类文件和 XML 观察器部分中的
Nate_SearchToFind_Model_Observer
。Zend Framework 中的类名称遵循目录结构。您尝试使用的类前缀
Nate_SearchToFind_Bundle_Model
实际上是指app/code/{core,local,community}/Nate/SearchToFind/Bundle/Model
中的文件,我相信。需要将其更改为Nate_SearchToFind_Model
以反映您当前的目录结构。您还定义了类前缀,但没有使用它。例如,观察者部分的
部分可以读取natesearchtofindbundle/observer
,它将映射到Nate_SearchToFind_Model_Observer
,假设您将前缀与目录结构对齐。Your observer class name is wrong. It should be
Nate_SearchToFind_Model_Observer
in the PHP class file and the XML observer section.Class names in the Zend Framework follow the directory structure. The class prefix you are trying to use,
Nate_SearchToFind_Bundle_Model
, actually refers to files inapp/code/{core,local,community}/Nate/SearchToFind/Bundle/Model
, I believe. It needs to be changed toNate_SearchToFind_Model
to reflect your current directory structure.You're also defining the class prefix, but not using it. For example, the
<class></class>
section of the observer section could read<class>natesearchtofindbundle/observer</class>
, which would map toNate_SearchToFind_Model_Observer
, assuming you aligned the prefix with your directory structure.