MVC Architecture
MVC stands for Model-View-Controller. It's a way of organizing an application's code by splitting three separate concerns apart, instead of mixing database queries, HTML, and request-handling logic all in one file. CakePHP is built around this pattern from top to bottom.
The Pattern, in General
| Piece | Responsibility |
|---|---|
| Model | Data and business logic — talking to the database, applying rules |
| View | Presentation — turning data into HTML (or JSON, etc.) the user sees |
| Controller | Request handling — reading input, calling the model, choosing a view |
None of these three pieces should know too much about the others' internal details. A model shouldn't format HTML, and a view shouldn't run database queries directly.
Models: Table and Entity Classes
CakePHP splits "Model" into two related class types, both living under src/Model/:
- Table classes (
src/Model/Table/) — represent an entire database table, such asArticlesTablefor thearticlestable. Queries, validation rules, and save logic live here. - Entity classes (
src/Model/Entity/) — represent a single row, such as oneArticle. They hold the data for that one record and any row-specific behavior.
Views: Templates in templates/
Views are plain PHP template files stored under templates/, organized into a
folder per controller. A controller passes data to a view, and the view's only job is to
render that data as HTML — no database access, no business rules.
Controllers: Handling the Request
Controllers live under src/Controller/. A controller receives the incoming
request, asks the relevant model for data, decides what should happen next, and hands data
off to a view to render. The next two lessons cover routing and controllers in much more
depth.
Putting It Together: The Request Flow
A single request through a CakePHP application generally flows like this:
Request -> Router -> Controller -> Model (data) -> View (render) -> Response
The router decides which controller and action should handle the URL, the controller asks a model for whatever data it needs, and then a view renders that data into the final response sent back to the browser.