MVC in simpler terms or the structure of a modern web-application

  1. The Model
  2. The Router
  3. The Controller
  4. The View
  5. The Command
  6. Comments (8)

MVC is one of the most controversial terms in the PHP world.

Whereas it's really simple as long as you get the main principle: the Model is 90% of your application.

The Model

Thanks to the first generation frameworks, the Model is mostly associated with a database layer. Which is obviously wrong. The Model is whole your application actually, just without the interface. So it includes a database layer but also all the logic that provides the exact data to be returned to the client or methods to manipulate the data in the database.

I do realise that this concept is hard to grasp at first, but it will become much clearer the day you will need to create a command-line version of some action usually taken through the web-interface. Such as trivial task as creating a new user for example. Immediately it will become apparent, that both the Controller (the web interface) and the Command (the command line interface) are actually extremely thin layers, just interfaces that convey your actions to the Model.

No kidding, this is a real good test for your controllers. Just try to create a command line interface for creating the new user and see, whether you will need any code borrowed from the controller. If so -your Controller is fat and this code should be moved into the model.

Just a couple such cases and for the rest of your life you will stop making fat controllers that take up some of the responsibilities from the Model and thus make it hard to call the same functionality through any other interface.

So in a nutshell, your Model should be able to perform any task your application is supposed to do, no matter through which interface it is called.

Given the Model is up to 90% of your application, it is not unnaturally that itself it is often divided into different parts depends on the different areas of responsibility.

The Entity or a Data Mapper

This is what most of time is mistakenly taken for the Model - the basic mapping from a database table to a data object, which implements basic data manipulation methods also called CRUD.

The Repository

This is mostly a collection of complex SQL queries, that do not fit for the Entity. The source of any data your application may be in the need for.

The Helper

This is the place where the code from your fat controller should go. Like Repository, this is a general purpose script but unlike Repository it doesn't run any SQL query (calling either an entity or a repository for this), performing any data preparation necessary. For example if the data needs to be combined from different repositories, one could call corresponding methods from these repositories and then combine the data in the any fashion appropriate.

The Router

There is no R in the MVC acronym as this part is specific to the web-application only. The job of the Router is to determine which controller needs to be called to process the certain HTTP request.

The Controller

As it was mentioned above, the Controller should be a really thin layer whose the only job is to take the request parameters, send them to the Model for processing and then return some response back to the client (directly of by calling the View).

The View

A rather less important module that is called by the Controller, responsible for the HTML rendering. Obviously not used with Commands but also never used if a controller has been called through REST request, as in such a case instead of calling the View the Controller will simply encode the data into JSON format and simply return it.

The Command

As it was noted above, the Command is just a command-line interface that may duplicate some actions that usually performed by a controller, but of course could be specific to a command. Usually it is some maintenance tasks performed by a site admin through command line or via the cron job.


Related articles: