cancel
Showing results for 
Search instead for 
Did you mean: 

Plugins vs Observers, which one to use?

Plugins vs Observers, which one to use?

Hi Magento Experts,

 

Apologies in advance if my question sounds basic. I am new to Magento world. 

 

I am building an extension for Magento 2 and one of the goals is to push some entities (Customers, Products etc) into our system by hitting HTTP APIs from Magento. I was doing some investigations about the differences between Observers and Plugins (Interceptors) to see which one fits the purpose, but the more I read the harder it gets to decide. As far as I can tell there are two different viewpoints on the subject: 

 

  1. Observers are old and you should avoid using them. Magento will retire them in the future, and you should use Plugins instead.
  2. It's just the matter of choosing the right tool for the job. Use Plugins when you want to modify the data and use Observers when you don't need data modification. 

 

Which approach would you take if you were going to build a greenfield extension today?

 

Also, if you think Observers are still relevant, given my use-case which is hitting external HTTP APIs, what would be the pros and cons of using Observers vs Plugins? 

 

I would appreciate it if you could please help me understand this.

 

Regards,

Alex

3 REPLIES 3

Re: Plugins vs Observers, which one to use?

Core Magento 2 also has a lot of places where event observers play a prominent role in changing the logic of the application. The sheer number of places where observers work inside Magento means that it would be extremely difficult to get rid of them any time soon. 

 

There’s no doubt in the developer community that observers will eventually fade out of favor. But it will only happen when M2 completely abandons inheritance in favor of composition which is not the matter of the near future.

 

i. Use plugins with @api methods over simple plugins. @api methods will not get changed often by the Magento core dev team and thus are more reliable and future-proof.
ii. Event observers will remain your second best choice especially if you are well-acquainted with them from the M1 dev times.
iii. Non-@api plugins should be used last. They offer less stability than event observers and don’t have the advantages of @api plugins.

Re: Plugins vs Observers, which one to use?


@My Estub wrote:

Hi Magento Experts,

 

Apologies in advance if my question sounds basic. I am new to Magento world. 

 

I am building an extension for Magento 2 and one of the goals is to push some entities (Customers, Products etc) into our system by hitting HTTP APIs from Magento. I was doing some investigations about the differences between Observers and Plugins (Interceptors) to see which one fits the purpose, but the more I read the harder it gets to decide. As far as I can tell there are two different viewpoints on the subject: 

 

  1. Observers are old and you should avoid using them. Magento will retire them in the future, and you should use Plugins instead.
  2. It's just the matter of choosing the right tool for the job. Use Plugins when you want to modify the data and use Observers when you don't need data modification. 

 

Which approach would you take if you were going to build a greenfield extension today?

 

Also, if you think Observers are still relevant, given my use-case which is hitting external HTTP APIs, what would be the pros and cons of using Observers vs Plugins? 

 

I would appreciate it if you could please help me understand this.

 

Regards,

Alex


Compared to event observers, plugins can function anywhere in the system. They can be used to change or override any public method in Magento. Plugins are a valuable tool in custom development. For example, they are not affected by new Magento patches.

 

Re: Plugins vs Observers, which one to use?

Thanks for the response mate