我想做一些数据库操作,并在首页加载之前获取/设置一个cookie。我将以哪个事件成为观察者?我还没有在我的研究中创建cookie,现在我想在活动中做一些数据库操作。我现在有一个观察员在controller_front_send_response_before
走了,这是我得到的错误跟踪。
Mage registry key "_singleton/" already exists
Trace:
#0 /var/www/html/app/Mage.php(192): Mage::throwException('Mage registry k...')
#1 /var/www/html/app/Mage.php(446): Mage::register('_singleton/', false)
#2 /var/www/html/app/code/core/Mage/Core/Model/App.php(1252): Mage::getSingleton(false)
#3 /var/www/html/app/Mage.php(416): Mage_Core_Model_App->dispatchEvent('controller_fron...', Array)
#4 /var/www/html/app/code/core/Mage/Core/Controller/Varien/Front.php(186): Mage::dispatchEvent('controller_fron...', Array)
#5 /var/www/html/app/code/core/Mage/Core/Model/App.php(340): Mage_Core_Controller_Varien_Front->dispatch()
#6 /var/www/html/app/Mage.php(627): Mage_Core_Model_App->run(Array)
#7 /var/www/html/index.php(86): Mage::run('', 'store')
#8 {main}
这是我的配置节点:
<frontend>
...
<events>
<controller_front_send_response_before>
<observers>
<type>singleton</type>
<class>Foo_Bar_Model_Observer</class>
<method>controllerFrontSendResponseBefore</method>
</observers>
</controller_front_send_response_before>
</events>
</frontend>
这是我的观察者模型
class Foo_Bar_Model_Observer
{
public function controllerFrontSendResponseBefore(Varien_Event_Observer $observer)
{
$id = 1;
$theVisitors = Mage::getModel('visitors/visitor');
$theVisitors->load($id);
$numVisits = (int)$theVisitors->getNumvisits();
++$numVisits;
$theVisitors->setNumvisits($numVisits);
$theVisitors->save();
}
}
答案 0 :(得分:1)
请参阅Mage_Core_Model_App::dispatchEvent()
中的switch语句。您没有在观察者配置中声明<model>
节点 - 您有一个<class>
节点。 有趣的是,Mage::getSingleton()
的编码很差。查看是否应该创建和注册单例实例的测试不能处理_singleton/
的原型键!
现在为你的cookie。嗯饼干。您正在观察cookie操作的正确事件(在Mage_Core_Controller_Varien_Front::dispatch()
的评论中很明显)。您还可以观察controller_action_predispatch
。但是,这些将触发每个页面视图,因此您需要在观察者中实现首次访问者逻辑。此外,您可能在这里有一些愚蠢的代码作为示例,但显然首页不是唯一的初始入口点(我假设您将使用cookie进行操作)。 CMS路由器中有一个易于定位的事件Mage_Cms_Controller_Router
- 您可以将其用于主页视图。
我不应该告诉你有一个访客记录机制(参见log_*
表)。
答案 1 :(得分:1)
您是使用社区版还是Magento Enterprise?如果您使用的是Magento Enterprise,并且页面由完整页面缓存模块(Enterprise_PageCache)完全提供服务,则不会触发任何事件。这意味着只有在没有(完整)缓存可用时才会触发您的代码。以下访问者将从缓存中获取网站,因此不会触发任何事件。
原因是,在启用FPC(并已启动)的情况下,Magento会在不实际运行完整应用程序的情况下提供所有内容。这也导致一个空的观察者集合,因此无需派遣。您的自定义观察者不会被触发。
据我所知,在FPC模式下工作的唯一方法是为每个唯一用户打孔,以强制Magento初始化应用程序,从而触发流程中的所有常规事件并获取自定义代码。 / p>
如果您经营社区,您无需担心。除非您还为TinyBrick的LightSpeed模块等社区运行FPC变体。
答案 2 :(得分:0)
此类问题与编译magento
有关应考虑进行编译
转到settings / tools / compiler或命令行:php shell / compil.php - compile
哈蒂嘉