In my CakePHP app, I want to build a plugin or component that would take my data array before save, see if there are files to be uploaded (I will check for fields named attachment, image, thumb and gallery), upload them, create thumbnails if needed, write entries in the appropriate tables and modify the data array with the entry IDs.
I will use this in several models so I want to make a reusable code (I already have it working as functions in AppModel but it is quite chaotic and I don't like it).
Should I make a component that will connect to several Models, or should I make a plugin that will contain the component and the models?
I like the second option better, but I don't know how to pull this out (how to load plugin models in the plugin component, or call plugin controller actions from the plugin component without redirect). I also don't know if this is the right way to do it.
This is a very common question, as it takes some times to learn the difference between Components, Behaviors, Plugins, and Elements. For that reason, I extended this answer a bit to explain them all - once you understand them, this question is self-answering.
Because of your mention of "before save" trigger, this means that you'll most certainly want a Behavior because they have reusable call-back methods.
The question then is, "Do I need anything MORE than what a Behavior can provide?". If the answer is yes, then you can make a Plugin that can include any additional things you need.
If the answer is no, then there's not much reason to make an entire Plugin just for a single Behavior.
UNDERSTANDING Components, Behaviors, Plugins, (and Elements)
Components:
Components are packages of logic that are shared between controllers.
That about sums it up really - Components are used for logic that you want to share amongst Controllers. That's the key - for "Controllers". These aren't used for database-data-related modifications, because that should be in the Model (per MVC standards).
An example of a component would be if you have a large amount of logic related to file-uploading that you want to use in more than one Controller. It's not directly database-related (which would be a Model), and it's not for creating HTML (see Views) - it's common logic that you want to share w/ other Controllers.
Behaviors:
Model behaviors are a way to organize some of the functionality defined in CakePHP models.
Behaviors are most commonly used to allow multiple Models to share callback methods. For example a "Sluggable Behavior" could have a "beforeSave()" call back that takes whatever is in the "name" (or other) field and turns it into a slug and puts it into the "slug" field. Then, any Model that actsAs that Behavior will automatically get a slug populated whenever it saves.
Plugins
Plugins are a combination of controllers, models, and views released as a packaged application plugin that others can use in their CakePHP applications.
A Plugin is always an option regardless of what you're trying to do, but the point of a Plugin is usually a way to package models, views, controllers, components, behaviors...etc into something that you can use across multiple projects. If you're certain those things are for just one project, you could have those same files just in the project itself - no need for a plugin. But if there's a chance you want it reusable easily, then you can put them into a Plugin.
Elements
An element is basically a mini-view that can be included in other views, in layouts, and even within other elements. Elements can be used to make a view more readable, placing the rendering of repeating elements in its own file.
An element is the only one of these that is View related. For example a small module box that shows your contact form that you want to put on lots of pages (possibly in different spots).
If it's on EVERY page, you might think about just including it in your Layout file, but if it's something that may or may not be there, and/or might be in a different location..etc, then an Element is a good way to make reusable View code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With