Part 4 is another pretty straighforward section, how to add an event observer in Magento 2. Yes event-dispatch is still present in Magento even with the introduction of plugins (which I’ll come onto at some point).

There are two steps to creating an observer once you’ve decided which event you want to hook into. First you declare it in a brand-spanking new XML file and then you create the observer class, a separate class for each observer!

Step 1 - Create Magefox/Example/etc/frontend/events.xml

In this example we’re dealing with a frontend event (meaning that the same event fired in the admin would have no implications).

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_postdispatch">
        <observer name="magefox.fire" instance="Magefox\Example\Observer\Fire" />
    </event>
</config>

In the example above we’ve simply created a new hook to look out for the execution of controller_action_postdispatch on the frontend - a fairly common occurance I would imagine! Then we’ve told it to run the script in our observer, which we’ll now create below.

Step 2 - Creare Magefox/Example/Observer/Fire.php

This is the most simple of observers, no dependencies, just an execute method like those found in Magento 2 controllers.

<?php
namespace Magefox\Example\Observer;

use Magento\Framework\Event\ObserverInterface;

class Fire implements ObserverInterface
{
    /**
     * Test observer to echo "Done"
     *
     * @param \Magento\Framework\Event\Observer $observer
     *
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "Done";
    }
}

“Done” will now be echoed annoyingly at the top of the page when the event is dispatched. As with Magento 1 observer data can be accessed with $observer->getEvent()

That’s all folks!

Part 5 - Creating an admin page »