We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Plugin vs Component: what's the difference?

What is the difference between Phalcon\Mvc\User\Plugin and Phalcon\Mvc\User\Component?



29.4k

Hi, I've seen this discussion

Phalcon\Mvc\User\Plugin is recommended being used in plugins and Phalcon\Mvc\User\Component in user components

Ok. But what is Plugin and what is Component?



98.9k

A plugin is like the Security plugin in INVO (https://github.com/phalcon/invo/blob/master/app/plugins/Security.php#L13), it implements methods as events of some component, in this case Dispatcher (https://github.com/phalcon/invo/blob/master/public/index.php#L43)

A component is a class or group of classes that provide custom functionality to the application but aren't aware of events, like the Elements class: https://github.com/phalcon/invo/blob/master/app/library/Elements.php



29.4k

That's the only difference? So Plugin is some kind of the events listener and Components is for for all other purposes (including user interface)?



98.9k

Yep, a plugin is like a listener, however both classes don't have any real differences, they extend from Phalcon\DI\Injectable to easily access the service locator



29.4k

Thanks! @boston perhaps it is necessary to say more about this in the docs



8.1k

I concur with Agent-J, there is no documentation about the Component/Plugin

How about controllers? Are they act like plugin? (In some case, they concern with event like beforeExecute Route etc)

Controllers handles the application login in the MVC but again can be used as a plugin if registered in the services using the di component.

Just as it sounds. The way I understand plugins and components is that, component can be used independently but plugins (as they sound) have to be plugged in a component or module so to speak in order to execute. That's why plugins rely heavily to events (action from the user) on run time. Plugins can extend the functionability of a component by adding some behaviours. A good example can be; A car (component) can be driven when it is raining(event) without wipers(plugins). But with wipers the car can be driven much better due to behavior change in weather.

From this anology plugin can make a component fit in different usability while reducing the number of bugs.