Get Free Audit

Declaring Observers in Magento 2.0 (Updated)

Jan 15, 2016

1373 Andrei Danilchyk

Declaring Observers in Magento 2.0 (Updated)

Registration and creation of observers in Magento 2.x is a bit different from the version 1.x.

  • a separate events.xml file is used for registering observers;
  • the scope of the observer is defined not by the xml node, but by the location of the events.xml file;
  • XML syntax is slightly different from the version 1.x.

Let’s take a closer look at these differences using our module Age Verification Page as an example.

Unlike in Magento 1.x where all events are registered in the common config module, in Magento 2.x all module observers are stored in a separate xml file under the following path:

app/code/BelVG/Verpage/etc/events.xml(app/code/BelVG/Verpage/etc/adminhtml/events.xml

or

app/code/BelVG/Verpage/etc/frontpage/events.xml

Let’s take a look at the xml file:


The attribute ‘name’ in the ‘event’ node defines the event when the observer should be called.

magento development services

Magento 2 Development

Take your online store to the next level with BelVG Magento 2 Development

Visit the page

The “observer” node defines the observer itself and its attributes:

  • name – the observer registration name (it is important that the names do not coincide);
  • instance – the class, which method will be executed when a specific even occurs;
  • method – the method being executed.

Let’s take a look at the observer (app/code/BelVG/Verpage/Model/Observer.php):


As you see, there have been very few changes compared to Magento 1.x except for the class names. In our case the function ‘verificate’ receives the current observer object as an argument.

In the beginning we also mentioned one more important difference – the definition of the scope of the observer. Unlike in Magento 1.x:

The file config.xml:

Igor Dragun
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor


In such case Magento 1.x defines the observer only on the frontend.

In Magento 2.x the scope of the observer is defined by the location of the events.xml file in the specific folder.

Frontend location: app/code/Namespace/Modulename/etc/frontend/
Admin location: app/code/Namespace/Modulename/etc/adminhtml/

If the observer is required both on the frontend and in the admin panel: app/code/Namespace/Modulename/etc/

magento webdesign

Magento Webdesign

Take your online store to the next level with BelVG Magento Webdesign

Visit the page

Update

In the final version of Magento Community Edition (CE), several changes have been included:

app/code/BelVG/Infscroll/etc/frontpage/events.xml (for example, Infinite Scroll extension)


The “method” parameter has been deleted and now observers represent something like a controller (that has an additional “execute” function).
Accordingly, the following changes in the observer have been made:

  1. All observers have been moved to the root catalog of the extension
  2. The observers are now made similarly to controllers.
Igor Dragun

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor

Example: app/code/BelVG/Infscroll/Observer/AjaxRenderBefore.php

namespace BelVG\Infscroll\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\DataObject as Object;

Igor Dragun
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor

7 Comments

  1. Firstly to let you know that the namespace and use statements in the update are not in the ‘code’ formatting :)

    Also thank you for posting as this helped me fix a 3rd party extension which had not been updated.

    Nice one!

  2. Hi Karthick, make sure you are doing something like this to use logger in your observer:

    protected $_logger;
    public function __construct(
    \Psr\Log\LoggerInterface $logger
    ) {
    $this->_logger = $logger;
    }

    and then call it: $this->_logger->info('some message here',true);

  3. Hi andrei,
    I tried this observer in magento2 with putting logs in the observer method, but it seems observer cant firing in my method my events and observer function are

    _registry = $registry;
    $this->_logger = $logger;

    }

    public function message(\Magento\Framework\Event\Observer $observer)
    {
    $this->_logger->addInfo(‘some text or variable’);
    //$logger->info(‘Informational message’);
    }

    }

    please check it and let me know whats the problem here. I cant able to see any logs also

  4. There is a change in observers.
    Now you should not declare the attribute method and they automatically dispatch the method execute.

    Regards

  5. Thank you for such useful post.
    Here, I have one pretty question I have.

    In magento 2 where I can get list of events/observers?

    Thanks,
    Praful

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *