The manual for CakePHP has a pretty basic explanation of what MVC is. This article will explain things in a little more detail because the MVC pattern is crucial when it comes to using CakePHP. If you are not familiar with the MVC design pattern, read on…
What is MVC anyway?
MVC stands for Model-View-Controller. MVC is a well known design pattern that basically keeps that data handling, application flow, and presentation seperate from each other. At first it may seem a little confusing, but it makes coding much easier and efficient. I will explain each aspect of the MVC below relative to a simple application to manage contacts.
The model is responsible for handling all of the data. In CakePHP each model usually corresponds to a table in the database. So if in our database we have a table named “contacts” to store all of our contacts then there will be a model named “Contact”. The plural and singular convention is important but I will not be explaining it at this time. I recommend you read the CakePHP conventions before you actually start using Cake because they will save you time and headaches. You can just trust me that the model name should be singular, and the database table should be plural. Here is a rough example of what a model would look like in CakePHP. This file would be stored at “/app/models/contact.php”:
class Contact extends AppModel
var $name = 'Contact';
This is actually all you need to get started with a basic model in CakePHP. As you can see the class Contact extends the class AppModel so it inherits all of the functions that AppModel has access to. The AppModel has functions like find, save, delete, etc that allow you to interact with the database. The model should be responsible for all interaction with the database.
So in review, anything to do with manipulating data should be done in the model. Whether it is a query to CRUD (create, read, update, delete) operations to the database you should be doing it from the model. Extending Cake’s AppModel class allows you to use the built in functions for CRUD operations so that you should never have to write an SQL query again! If you would like to learn more about the model and the functions available to it check out the Models page in the Cookbook.
The view is what it sounds like. It is the presentation layer, everything that has to do with displaying the data is done in the view. In cakePHP the view is set up in templates that displays data that is obtained from the model. The view should be the only place where HTML resides. This makes things a lot easier when it comes to modifying the look and feel of an application without having to touch the guts. The view is basically a template and in CakePHP they have the extension “ctp”. Here is what and example of what the view would look like to display a list of contacts… lets call it “index.ctp”. In an application setting this file would be stored in the folder “/app/views/contacts”.
<?php foreach($contacts as $contact): ?>
<?php echo $contact['Contact']['name']; ?>
<?php endforeach; ?>
Of course this is an extremely basic example of printing out a list of contact names. In the view we would definitely want to present more information in a nicer way… the best part is in order to do that the only file we will have to edit is the view. You’re probably wondering where the $contacts variable came from, this brings us to the controller.
Lastly, there is the controller. The controller manages the flow of the application. It is responsible for handling requests and passing the proper information for the given request. The controller consists of several functions, called “actions”, which determine what should be done. Each action (usually) has an associated view. So for example, if we have an action called “index” in our controller we will need a view called “index.ctp” that displays the information for the given action. The view in the example above uses a variable called $contact. That variable is set using the controller. Here is an example that ties in with the above view, this file would be “/app/controllers/contacts_controller.php” in an application setting.
class ContactsController extends AppController
var $name = 'Contacts';
$contacts = $this->Contact->find('all');
The function in the controller above asks the Contact model to find all of the entries in the associated contacts table and then assigns it to the variable $contacts. Afterwards the controller sets the variable $contacts so that the view can now use the $contacts variable. The built in libraries will automatically search for the file “/app/views/contacts/index.ctp” and use it to display the information passed. The best convention is to keep the code in the controllers to a minimum and instead put the bulk of the code for manipulating the data into the model (see Fat Models, Skinny Controllers by Chris Hartjes).
Putting It All Together
So there you have it, we just created an extremely simple Model, View, and Controller for listing contacts. Of course from here you would build on creating actions in the controller for add, view, edit, etc. and views to go with those actions. But hopefully this gives you some insight in to how it works. If you have never followed this design practice before you are probably wondering some of the benefits of the MVC practice. It may not be immediately apparent, but there are several. First of all, separating code in this method allows the basic functionality of the code to be reused over and over again. When it comes to modifying the way the application looks you only have to edit the views instead of all of the code. This allows someone who is familiar with HTML to customize the look of things without having to search endless lines of code that may not be familiar to them. It also makes debugging much easier because you can pinpoint exactly where the problems may be coming from. If the data isn’t correct, it must be the model… If something is funky with the way it is displayed, it has got to be the view… and so on. This is of course just one of the great design patterns used by Cake and there are several others that will save time and headaches.
Additional MVC Articles