This article is designed to help a user understand basic features in CakePHP. The documentation tends to go into great detail without first setting up a framework.
Where should my code go?
- Note: You can also find this article in the bakery.
The relative sizes of the various layers of your application. Remember
Fat model, skinny controller source
Here are some tips to help you decide where code should go. The "take away" message for each part is highlighted in bold.
Before reading this, make sure you understand the model-view-controller design pattern.
Models
- Models are designed to hold and manipulate data
- Models essentially reflect a database table
- Models never represent a row in a table, just the table itself
- Models have functions that deal with manipulating data from the table
- Models should be large and should contain the majority of the logic for your application (fat models, skinny controllers)
- Anytime you are doing the same thing to a model in multiple places, you should refactor that functionality into a method within the model (instead of having it in multiple places outside the model).
- Cookbook: Understanding Models
Behaviors
Controllers
- Controllers are designed to separate your site into sections
- Examples: BlogController, SearchController, GroupsController, UserProfileController
- Contrary to what the blog tutorial makes you think, there is not a single controller for every model (or a single model for every controller). It is fairly common that a controller will hold all the tasks of a single model, but it is NOT necessary for things to be that way.
- On the same token, avoid mixing controller uses. While there may not be a controller for every model, don't put actions that deal with model B into model A just to avoid having to create a controller
- Controllers should be small and NOT contain the majority of the logic for your application (fat models, skinny controllers)
- The controller isn't there do do everything. It is there to determine what needs to be done, and have other people do it
- Controller actions get data ready to be presented in a view
- Cookbook: Introduction to Controllers
Components
- Components have logic that is shared across multiple controllers
- Remember, logic should only be in multiple controllers in the first place if it is getting data ready for the view. If the logic is just manipulating your data, it should be in a model.
- Cookbook: Introduction to Components
Views
- Views are individual pages within your site
- Views are the user interface to your program
- Each view has a function inside the controller with a matching name. These functions are called actions. Each action gets the data ready for a specific view.
- Views presents data
- Views allow you to separate your user interface from the rest of your application
- Cookbook: Introduction to Views
Helpers
Elements
Development tips
Please just drop any helpful links regarding CakePHP on this page.
Deployment and Setup
Plugins
Using CakePHP
Other Tutorials
Misc
- Whenever you need to paste code to show someone else, use CakeBin.