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:
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8"?> ... <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd"> <event name="controller_front_send_response_before"> <observer name="belvg_verpage_controller_front_send_response_before" instance="BelVG\Verpage\Model\Observer" method="verificate" /> </event> </config> |
The attribute ‘name’ in the ‘event’ node defines the event when the observer should be called.
Magento 2 Development
Take your online store to the next level with BelVG Magento 2 Development
Visit the pageThe “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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php ... namespace BelVG\Verpage\Model; class Observer { protected $_verpageData; protected $_registry = null; public function __construct ( \BelVG\Verpage\Helper\Data $verpageData, \Magento\Framework\Registry $registry ) { $this->_verpageData = $verpageData; $this->_registry = $registry; } public function verificate(\Magento\Framework\Event\Observer $observer) { ... } ... } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<frontend> <events> <belvg_jquery_dispatch_before> <observers> <belvg_verpage_jquery_dispatch_before> <type>singleton</type> <class>verpage/observer</class> <method>verificate</method> </belvg_verpage_jquery_dispatch_before> </observers> </belvg_jquery_dispatch_before> </events> </frontend> |
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
Take your online store to the next level with BelVG Magento Webdesign
Visit the pageUpdate
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)
1 2 3 4 5 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="layout_render_before"> <observer name="belvg_infscroll_layout_render_before" instance="BelVG\Infscroll\Observer\AjaxRenderBefore" /> </event> </config> |
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:
- All observers have been moved to the root catalog of the extension
- The observers are now made similarly to controllers.
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.Example: app/code/BelVG/Infscroll/Observer/AjaxRenderBefore.php
namespace BelVG\Infscroll\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\DataObject as Object;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
class AjaxRenderBefore implements ObserverInterface { const AJAX_PARAM_NAME = 'infscroll'; const AJAX_HANDLE_NAME = 'infscroll_ajax_request'; /** * Https request * * @var \Zend\Http\Request */ protected $_request; protected $_layout; protected $_cache; /** * @param Item $item */ public function __construct( \Magento\Framework\View\Element\Context $context, \BelVG\Infscroll\Helper\Cache $cache ) { $this->_layout = $context->getLayout(); $this->_request = $context->getRequest(); $this->_cache = $cache; } /** * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { /* function body */ } } |
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!
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);
Guys asking for the list of events fired in Magento 2, you can get the full list here
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
There is a change in observers.
Now you should not declare the attribute method and they automatically dispatch the method execute.
Regards
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
Where can i found list of event name in magento 2 ?