MVC Pattern in PHP (How Laravel/Symfony Think)
Understand Model-View-Controller with a clear mental model and a simple PHP example.
MVC is a design pattern that separates responsibilities:
- Model: data and business rules (database logic) - View: HTML templates - Controller: receives request, calls model, returns view
MVC flow (easy picture)
```mermaid flowchart LR A[Browser Request] --> B[Controller] B --> C[Model] C --> D[(Database)] C --> B B --> E[View] E --> A ```
Why MVC makes apps easier
Without MVC, pages become messy: - SQL inside HTML - logic everywhere - hard to maintain
With MVC, each part has a clear job.
Mini example (concept)
- `PostController.php` handles request - `PostModel.php` fetches posts - `posts.view.php` renders HTML
You do not have to build a full framework to benefit from this structure.
> Next: Build a simple JSON API in PHP (REST basics).