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

Difference between Phalcon\Mvc\User\Component. Plugin, and Module?

I'm looking at these below:

https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_User_Component.html https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_User_Module.html https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_User_Plugin.html

1: They all have the same features, and I'm guessing they are just named differently for organization. Is it correct to assume that?

2: Is the purpose of these are to have functionality outside of Models/Controllers that still have access to system services registered in the bootstrap?



5.3k
Accepted
answer

TL;DR - It's only a semantic difference at the moment, but it could change in the future. As far as the difference, this is what I've noticed in the places I've worked:

modules - Self-contained, reusable, collection of classes - including models, views, and controllers - created to perform a certain set of actions. For example, an admin module that has it's own MVC structure that can be used on a variety of projects.

components - An independent class or set of classes designed to extend the base functionality of a framework or application. For instance, I created a locale class for Phalcon that finds the user's best locale based on an array of available locales in the config. I extended Phalcon\DI\Injectable, but I probably should have used Phalcon\Mvc\User\Component. If I add any event logic (perhaps a pre.dispatch listener) I'll change it over.

plugins - Add a limited, specific capability to an existing application or framework component. I'm kinda wondering if Phalcon\Mvc\User\Plugin is really needed as most plugins extend the class it's "plugging into". For instance, if you are making a Phalcon\Tag plugin, you extend that class, not Phalcon\Mvc\User\Plugin (https://docs.phalcon.io/en/latest/reference/tags.html#tag-service). When I wrote an scrypt hashing plugin, it extended the Phalcon\Security class.

@Phalcon, are you shaping this framework to follow the same lines, or do you have a different philosophy?



98.9k

@Mark yes, those are exactly my thoughts, thanks for your answer

TL;DR - It's only a semantic difference at the moment, but it could change in the future. As far as the difference, this is what I've noticed in the places I've worked:

modules - Self-contained, reusable, collection of classes - including models, views, and controllers - created to perform a certain set of actions. For example, an admin module that has it's own MVC structure that can be used on a variety of projects.

components - An independent class or set of classes designed to extend the base functionality of a framework or application. For instance, I created a locale class for Phalcon that finds the user's best locale based on an array of available locales in the config. I extended Phalcon\DI\Injectable, but I probably should have used Phalcon\Mvc\User\Component. If I add any event logic (perhaps a pre.dispatch listener) I'll change it over.

plugins - Add a limited, specific capability to an existing application or framework component. I'm kinda wondering if Phalcon\Mvc\User\Plugin is really needed as most plugins extend the class it's "plugging into". For instance, if you are making a Phalcon\Tag plugin, you extend that class, not Phalcon\Mvc\User\Plugin (https://docs.phalcon.io/en/latest/reference/tags.html#tag-service). When I wrote an scrypt hashing plugin, it extended the Phalcon\Security class.

@Phalcon, are you shaping this framework to follow the same lines, or do you have a different philosophy?

Nice, well explained. Btw Im new in phalcon :) thanks for your insights.