Does interception in magento2 replace observers in magento1?
I don't think interception is intended to replace event observers.
There are still events dispatched in the Magento2 code.
The interceptors are just a way to process the input or output of a method.
Or the behavior of a method (using the around
interceptor) but from what I understand this is a risky approach.
I think it's more of a way to avoid class rewrites as much as possible.
In Magento 1.x, if you needed to modify the result of a method you needed to override the class that contains and the method itself.
now you can simply use before
, after
or around
interceptors to manipulate the data.
With observers you can manipulate data in certain fixed points of a method, not the full method itself.
In my opinion, using observers when possible is still the best approach for customizing even in Magento 2.
Interception is evolution of events. Its goal is to allow a developer to write his code without thinking about extension points.
With events you have to think about places where you do Mage::dispatchEvent() so that your module will be customizable.
Interception allows you not to think about this. You just write your business logic. Every method call is an event. So if you write your code in a proper object-oriented way, it will be fully customizable by design.
But events provide higher level Service Provide Interface that talks business language. So events will stay in Magento 2.
Currently there is still the option to listen to specific events and I would suggest that the interceptors is just a replacement for rewrites. I would still use the events when there is one in place for your needs.
If you look at the current release some of the modules have events.xml
files that include the definitions.
<event name="cms_wysiwyg_images_static_urls_allowed">
<observer name="catalog_wysiwyg" instance="Magento\Catalog\Model\Observer" />
</event>