Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Kevin Schroeder:
Using events in Magento 2
Jan 07, 2014 @ 12:16:37

For the Magento 2 users out there, Kevin Schroeder has posted a guide that could be helpful in your work - using events, specifically how to add an observer on a pre-dispatch controller event.

With the minimal research I have done on Magento 2 recently the only thing I am apprehensive about is the invocation chain. Its purpose is to open the doors wide open to where you can hook your application in. [...] In terms of declaring an observer a lot has changed, and it’s for the better. [...] Now instead of silently overwriting the value you will get an error such as “Duplicate key-sequence ['controller_action_predispatch'] in unique identity-constraint ‘uniqueEventName’” if your observer has a duplicate name.

He includes the XML configuration you'll need to add a "Hello World" observer to the pre-dispatch controller event (that includes an "instance" path) and the code for the simple class' structure. He example just appends the string "Hello World" to whatever is being sent as a response. He also shows how to trigger them in the application via the event manager instance injected as a part of the observer creation.

tagged: #magento2 #event #tutorial #observer #predispatch #eventmanager

Link: http://www.eschrade.com/page/using-events-in-magento-2/