Menu

A plugin wraps a method, a decorator wraps a service: Magento 2 vs Laravel
📰

A plugin wraps a method, a decorator wraps a service: Magento 2 vs Laravel

DEV Community: laravel·Nikita Sirenko·2 days ago
#MljwBM54
#devtolaravel#devto#dev#plugin#magento
Reading 0:00
15s threshold

A Magento 2 plugin wraps one method. A Laravel decorator wraps the whole service. Same job, different size, and the Magento one has 3 edges worth knowing: one stops PHP, and 2 fail without a line in the logs.

How a Magento plugin works

In Magento 2 you declare a plugin in di.xml . Magento then generates an Interceptor into generated/code : a subclass of the original that runs your before, after or around code on one public method. Your code asks for the original class and gets the Interceptor.

Because the Interceptor is a subclass, PHP's own rules apply to it.

A plugin on a final class is a fatal error: "cannot extend final class". Without a compile it kills the first request that needs the class. With setup:di:compile it fails the build.

A plugin on a final or protected method does nothing. The generator only wraps public methods that aren't final or static.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.