I want to do some database operations and get/set a cookie before the front page loads. for which event would i make the observer? I haven’t gotten to the cookie creation yet in my research, right now i want to do some database manipulation at the event. I have an observer right now going off at controller_front_send_response_before and this is the error trace i’m getting.
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}
This is my config node:
<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>
And this is my observer model
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();
}
}
See the switch statement in
Mage_Core_Model_App::dispatchEvent(). You aren’t declaring a<model>node in your observer configuration – you have a<class>node. Interestingly,Mage::getSingleton()is poorly coded. The test to see if the singleton instance should be created and registered doesn’t handle the prototyped key of_singleton/!Now for your cookie. Mmm cookies. You’re observing a proper event for cookie manipulation (evident in the comment from
Mage_Core_Controller_Varien_Front::dispatch()). You can also observecontroller_action_predispatch. These will trigger for every page view, though, so you need to implement first-time-visitor logic in your observer. Also, you might have some dumbed-down code as an example here, but obviously the front page isn’t the only initial point of entry (I assumes that’s what you are going to do with cookies). There is an easily-targeted event in the CMS router,Mage_Cms_Controller_Router– you can use that for home page views.I’d be remiss to not tell you that there is a visitor logging mechanism in place (see the
log_*tables).