CakePHP from scratch: Basic principles


In my first tutorial I showed you how to install and configure CakePHP. Let me explain some more basics today.

I am taking examples from official CakePHP pages to show you how CakePHP Model-View-Controller pattern work. This examples explain the basic principles that you should try to remember. If you never used MVC pattern before it is a little bit confusing, but you will understand as soon as we begin developing a real life application in my next tutorial.

CakePHP follows the MVC software design pattern. Programming using MVC separates your application into three main parts:

  • 1. The Model represents the application data
  • 2. The View renders a presentation of model data
  • 3. The Controller handles and routes requests made by the client

CakePHP MVC pattern

Figure: 1 shows an example of a bare-bones MVC request in CakePHP. To illustrate, assume a client named “Ricardo” just clicked on the “Buy A Custom Cake Now!” link on your application’s home page.

  • * Ricardo clicks the link pointing to, and his browser makes a request to your web server.
  • * The dispatcher checks the request URL (/cakes/buy), and hands the request to the correct controller.
  • * The controller performs application specific logic. For example, it may check to see if Ricardo has logged in.
  • * The controller also uses models to gain access to the application’s data. Models usually represent database tables, but they could also represent LDAP entries, RSS feeds, or files on the system. In this example, the controller uses a model to fetch Ricardo’s last purchases from the database.
  • * Once the controller has worked its magic on the data, it hands it to a view. The view takes this data and gets it ready for presentation to the client. Views in CakePHP are usually in HTML format, but a view could just as easily be a PDF, XML document, or JSON object depending on your needs.
  • * Once the view has used the data from the controller to build a fully rendered view, the content of that view is returned to Ricardo’s browser.

Almost every request to your application will follow this basic pattern. I’ll add some details later on which are specific to CakePHP, so keep this in mind as we proceed.

Why use MVC?
Because it is a tried and true software design pattern that turns an application into a maintainable, modular, rapidly developed package. Crafting application tasks into separate models, views, and controllers makes your application very light on its feet. New features are easily added, and new faces on old features are a snap. The modular and separate design also allows developers and designers to work simultaneously, including the ability to rapidly prototype. Separation also allows developers to make changes in one part of the application without affecting others.

Stay tuned as we will continue the series with developing a real life web application.

About the author

Zvonko Biškup

A passionate web developer with more than 13 years experience in PHP and other web related technology. My main interest is WordPress and new PHP frameworks.
Owner of Influendo Programming.
Enjoys exploring new stuff and possibilities, using new technologies and just having fun developing awesome stuff.

By Zvonko Biškup