Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 432 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What do I use - a Plugin, Component, or Behavior?

#1
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.
Reply

#2
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][1]:**

> 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][2]).

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][3]) - it's common logic that you want to share w/ other Controllers.


**[Behaviors][4]:**

> 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][5]. 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][6]**

> 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][7]**

> 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][8], 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.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

[6]:

[To see links please register here]

[7]:

[To see links please register here]

[8]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through