If you are working with a CakePHP application and you’re about to add a new “feature,” you may be considering making it a Plugin. There are a lot of benefits to Cake plugins, including containing the feature all in one place and making it reusable between projects. However, they aren’t always the right tool for the job. Loadsys has been developing Cake applications for over 7 years, and we’ve collectively learned a few things about when it pays off to build a plugin and when it ends up hurting you. So here are a couple things to think about that can help you decide.
- If the new addition to the app is going to integrate with the the main app any significant way (presented in main navigation, or a CRUD stack in the admin portal for example) then it’s not a plugin; it’s a feature. Build it into the app.
- If your addition depends on data from tables (hasOne, hasMany, HABTM) in your main app in order to work, then it’s not a plugin; it’s a part of your app. Build it into the app.
- If your addition has its own front-end code (actions and views meant to be directly accessible), then it’s probably not a plugin; it’s a feature. Build it into the app.
When you find yourself doing anything from the list above, you are really working with a new core part of your application that should be integrated with the rest and not a stand alone reusable component. The result of these guidelines is that plugins are best used for programmer-facing features that you might want to re-use between projects: Components, Behaviors, Helpers, libraries, etc. Experience has taught us that trying to build an entire app out of plugins tends to lead to spending the majority of your time working around limits in each plugin, and figuring out creative ways to link them together while still maintaining the supposed “modularity” of each one.
On the other hand, building plugins as programmer-level tools that are never directly used by end users leverages Cake’s ability to pull in and alias things like Components, Behaviors and Helpers. It helps encourage their portability between apps and naturally maintains their loose coupling to each app. It avoids the ugliness of tying database tables to a plugin, which Cake handles relatively poorly.
As with everything there are certainly exceptions, but the times we have strayed from these guidelines have caused us many headaches over the years.