Is interception intended to be the preferred method when dealing with changing the behavior of an original method in Magento2 as observers was 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.

| improve this answer | |
  • 2
    Agree. Interception is lower level mechanism. But it does not replace events. Events provide higher level service provider interface – Anton Kril Sep 9 '15 at 17:30

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.

| improve this answer | |
  • Can you please get this important fact in the devdocs as its a source of confusion, especially seeing as you still have many events in M2. If we know events are dead we can code accordingly, I've not seen anywhere that mentions this in docs. In other languages there are reasons to use events sometimes and interceptors other times, I think you need to clarify that Magento 2 is different. – Karen Baker Sep 9 '15 at 15:57

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"  />
| improve this answer | |
  • In Magento-2 latest version observer pattern has been changed.Now you don't need to declare method attribute in events.xml,,It will automatically run execute method ,,same like controller. – Keyur Shah Nov 16 '15 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.