1

For some reason, I can't get the getEvent() or getRequest() to work in my observer.

<!--Create an event--> <events> <controller_action_predispatch> <observers> <example_observer> <class>example/observer</class> <method>controllerActionPredispatch</method> </example_observer> </observers> </controller_action_predispatch> </events> 

And here is the observer:

class MasteringMagento_Example_Model_Observer { public function controllerActionPredispatch($observer){ //die("testing"); //this runs fine $controllerAction = $observer->getEvent()->getControllerAction(); Mage::log($controllerAction->getRequest()->getParams()); } } 

The die("testing") fires fine, but I can't get the rest of the observer code to work.

2 Answers 2

2

The code is correct. I guess logging is not enabled in Admin - System - Config - Developer - Debug

You can force logging with setting 4th parameter in Mage::log() to true.

Example:

$controllerAction = $observer->getControllerAction(); Mage::log($controllerAction->getRequest()->getParams(), null, 'my.log', true); 

Just to note ... you can also write $controllerAction = $observer->getControllerAction(); (without getEvent)

3
  • Thanks. Still not working. For some reason this is what it logs (an empty array): 2017-06-13T10:15:47+00:00 DEBUG (7): Array ( ) Commented Jun 13, 2017 at 10:51
  • This is OK if your URL doesn't contain parameters ... try domain.com?param=1 and check the logs. Commented Jun 13, 2017 at 10:56
  • Thanks. I will play around with it and let you know. At least it is logging something Commented Jun 13, 2017 at 11:00
0

Try below code in config:

 <!--Create an event--> <events> <controller_action_predispatch> <observers> <example_observer> <class>MasteringMagento_Example_Model_Observer</class> <method>controllerActionPredispatch</method> </example_observer> </observers> </controller_action_predispatch> </events> 

And use the log in observer as shown below:

class MasteringMagento_Example_Model_Observer { public function controllerActionPredispatch($observer){ //die("testing"); //this runs fine $controllerAction = $observer->getEvent()->getControllerAction(); Mage::log($controllerAction->getRequest()->getParams()); Mage::log($controllerAction->getRequest()->getParams(), null, 'your.log', true); } } 
2
  • As he writes ... die() works, so the observer is correctly called. :) Commented Jun 13, 2017 at 10:48
  • yes. I have overlooked. Commented Jun 13, 2017 at 10:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.