HEMANT SONKER'S BLOG

Saturday, September 29, 2007

Cake PHP

Cake is a rapid development framework for PHP which uses commonly known design patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. The primary goal is to provide a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss to flexibility.

Cake is based on an MVC-like architecture that is both powerful and easy to grasp: controllers, models and views guarantee a strict but natural separation of business logic from data and presentation layers.

System requirements

Before you begin, you need to have an environment in which you can work. CakePHP has reasonably minimal server requirements:

  1. An HTTP server that supports sessions (and preferably mod_rewrite). This tutorial was written using Apache V1.3 with mod_rewrite enabled.
  2. PHP V4.3.2 or later (including PHP V5). This tutorial was written using PHP V5.0.4
  3. A supported database engine (currently MySQL, PostgreSQL or using a wrapper around ADODB). This tutorial was written using MySQL V4.1.15.

You'll also need a database ready for your application to use. The tutorial will provide syntax for creating any necessary tables in MySQL.

The simplest way to download CakePHP is to visit CakeForge.org and download the latest stable version. This tutorial was written using V1.1.8. (Nightly builds and copies straight from Subversion are also available. Details are in the CakePHP Manual.)


Controllers contain the logic of your application. Each controller can offer different functionality; controllers retrieve and modify data by accessing database tables through models; and they register variables and objects, which can be used in views.

Models are active representations of database tables: they can connect to your database, query it (if instructed to do so by a controller) and save data to the database. It is important to note that in order to correctly apply the MVC architecture, there must be no interaction between models and views: all the logic is handled by controllers.

Views can be described as template files that present their content to the user: variables, arrays and objects that are used in views are registered through a controller. Views should not contain complex business logic; 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.

This architecture can greatly improve the maintainability and the organization of your site's code:

  • It separates business logic from presentation and data retrieval.
  • A site is divided into logical sections, each governed by a particular controller.
  • When testing and debugging an application, any developer accustomed to CakePHP's structure will be able to locate and correct errors without knowing all of the details of the code.

Controllers, models and views are stored in pre-defined directories within CakePHP's directory structure. Here's the directory structure that's used:

  • app/
    • config/
    • controllers/
    • models/
    • plugins/
    • tmp/
    • vendors/
    • views/
    • webroot/
  • cake/
    • config/
    • docs/
    • libs/
  • vendors/


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home