• Your Cart is Empty
  • Cart
  • Log In


What is CakePHP framework?

CakePHP is a free open-source PHP development framework for PHP, based on an MVC-like architecture that is powerful, but also easy to understand. The CakePHP framework represents a foundational structure for programmers to create web applications. Using it, the developers are enabled to work in a structured and rapid manner, without losing flexibility. A main benefit of CakePHP is the presence of an active developer team and a solid community of users.

Latest version:
Multiple DB
DB Objects
Auth Module

CakePHP - an MVC-based framework

The CakePHP framework represents a robust base for handling every aspect of a web application, from the user's initial request to the final rendering of a web page. Following the principles of MVC (model, view, controller), the framework allows you to easily customize and extend most aspects of your application. It also offers a basic organizational structure, from filenames to database table names, keeping your entire application consistent and logical.

Beside Controllers, Models and Views, CakePHP features also Component, Behavior and Helper classes. We will try to explain the basic usage of the main components of the CakePHP framework in the examples below.


The controllers contain the logic of the application. Each controller can offer different functionality, retrieve and modify data by accessing database tables through models, register variables and objects, which can be used in views. Most often, they are used to manage the logic for a single model. In CakePHP, there are file and classname conventions. Thus, the way you name your files matters. For example, the Product model would be handled by the ProductsController (note the plural form), the controller would bear the filename products_controller.php, whereas the model itself would be product.php. All these conventions can be changed, but using them makes your life a lot easier.

Your application's controllers are classes that extend the CakePHP AppController class, which itself extends the Controller class. The AppController class can be defined in /app/app_controller.php. It should contain methods that are shared between all of your application's controllers.

Controllers can include any number of methods which are usually referred to as actions. Actions are controller methods for displaying views. The dispatcher calls actions when an incoming request matches a URL to a controller's action. For example, the URL http://domain.com/CakePHP/forms/index will call the function 'index' in the FormsController on domain.com.

We will create a simple page with a form that posts text into a MySQL database and lists all your posts.

The first thing we need to do is create the database that we will be using and edit the database configuration file /app/config/database.php. Then set up the table we will post our information in. This will require executing the SQL query:

#The name of the table is the plural form of the model that we will be using.
CREATE TABLE `forms` (
`id` int(11) NOT NULL auto_increment,
`data` text,

An example of creating a Forms Controller

The controller will be found in /app/controllers/forms_controller.php.

class FormsController extends AppController {

#Define the name of the controller
var $name = 'Forms';
#Define which model we will be using. This, by convention is Form and if our model is Form, then we can skip it.
var $uses = array('Form');
// This is the function that will be used when http://domain.com/CakePHP/forms/index is accessed
function index() {
#Check the ‘$this->data’ variable to see if something is submitted to the forms controller that uses the Form model.
#The $this->Form->save() method saves everything that is submitted to the database
#Display a confirmation message and redirect to the forms controller after 2 seconds
$this->flash('The form was successfully submitted.<br>You will be redirected within 2 seconds..','/forms/','2');
#The $this->Form->find(‘all’) method will get all the data from the database and assign it to the variable $data.
$this->set('data', $this->Form->find('all'));



Models are active representations of database tables and are used in CakePHP applications for data access purposes. They can connect to your database, query it in case they are instructed to do so by a controller, and save data to the database. A model usually represents a database table but can also be used to access anything that stores data such as files, LDAP records, iCal events or rows in a CSV file. In order for the MVC architecture to be correctly applied, there must be no interaction between models and views. Instead, all the logic should be handled by the controllers.

A given model can be associated with other models.

An example of creating a Form Model

The model will be found in /app/ models/form.php

class Form extends AppModel
#Define the name of the Model
var $name = 'Form';


Views are template files that present their content to the user. The variables, arrays and any other objects that are used in views are registered through a controller. Views should not contain complex business logic. Instead, only the elementary control structures necessary to perform particular operations, such as the iteration of collected data through a foreach construct, should be contained within a view. There must be a view defined for almost every function (action) defined in your controller. Since we only have an index() action, we will define only an index.ctp view.

An example of creating a Forms View

The view will be found in /app/views/forms/index.ctp

<div id="form">
#Here we loop through the $data array that we have defined in our controller
<?php foreach ($data as $d) { ?>
<td><?= $d['Form']['id'] ?></td>
<td><?= $d['Form']['data'] ?></td>
<?php } ?>
#Create a form using CakePHP’s integrated Core Helpers and define the action of the controller, which points the form to the page it will be posted to, through the options array.
echo $form->create('Form', array('action' => 'index'));
#Create the input using the Core Helpers again. The ‘type’ defines the type of the input field and the ‘label’ defines what will be written above the input.
echo $form->input('data', array(
'type' => 'text',
'label' => 'Add new entry:'
#Create a submit button and a </form> tag.
echo $form->end('Submit');

Now our form is created and is fully operational.


Helpers represent component-like classes for the presentation layer of your application, which contain presentational logic shared between many views, elements or layouts.

The CakePHP framework needs a controller in order to make usage of helpers. Each controller has a $helpers property that lists the helpers to be made available in the view. To enable a helper in your view, add the name of the helper to the controller’s $helpers array.

An example of using helpers in CakePHP - include Ajax and Javascript in the controller

class ExampleController extends AppController {
var $name = 'Example';
var $helpers = array('Html','Ajax','Javascript');


Model behaviors allow us to separate logic that may not be directly related to a model but has to be there and to attach functionality to models by defining a simple class variable. Behaviors enable models to get rid of the extra weight, which might not be part of the business contract they are modeling, or which is needed in different models and can be then extrapolated.


Components represent packages of logic that are shared between the controllers. If you want to copy and paste stuff between the controllers, you might consider wrapping some functionality in a component.

CakePHP comes with a set of core components you can use to aid in:

  • Security
  • Sessions
  • Access control lists
  • Emails
  • Cookies
  • Authentication
  • Request handling

The MVC architecture can greatly improve the maintainability and the organization of your site's code, whereas the usage of the inbuilt Helpers, Components and Behaviors helps even more. The CakePHP architecture allows you to separate business logic from presentation and data retrieval. Your PHP framework-built sites are divided into logical sections governed by a particular controller. When a developer accustomed to CakePHP's structure is testing and debugging an application, they will be able to locate and correct errors without knowing all of the code's details.