CakePHP Installation & Configuration

December 15th, 2010 Leave a comment 1 comment
Like the article?
CakePHP Installation & Configuration

In the past, we have discussed the importance of application frameworks. Frameworks make it easier to write maintainable code and they also provide a shortcut for rapidly developing new web applications. One of the more popular frameworks for use with PHP is CakePHP. CakePHP is a model-view-controller style framework. Structurally, it has many similarities to Ruby based Ruby on Rails web framework. CakePHP is one of the oldest and most robust and stable PHP frameworks on the market. CakePHP is free and open source. They have a manual called the Cookbook that provides some basic documentation. However, the Cookbook isn’t terribly detailed. Since installing CakePHP is the first step in getting familiar with it, we are going to walk through some basic CakePHP concepts and describe how to install a new CakePHP site using Apache, MySQL and Linux.

The Parts of CakePHP

As mentioned, CakePHP is a model-view-controller framework. Models in CakePHP handle all the database interaction. They save, update and query the database. Controllers host the logic of the application and control the input and output from the application. Controllers interact with the model layer to retrieve and save objects and they set variables for display in the view. In CakePHP, views are the templates that get rendered to display a web page or send an email.

In addition to the models, controllers and views, CakePHP also features helpers, components, elements and behaviors. Behaviors modify how models work and allow you to add additional functionality to your models. Components are pieces of code that are shared between many controllers. Some of CakePHP’s core components include code to manage sessions, cookies and authentication among others. Elements are small snippets of view code that can be rendered within other views. Elements provide a way to break out pieces of your view that are repeated on multiple pages. Helpers provide functions to simplify your development by creating shortcuts you can use in your views. The core helpers provide functionality to simplify the rendering of HTML, forms, JavaScript and email messages.

One of the ways that CakePHP helps developers write more maintainable code is by the use of conventions. CakePHP uses conventions to make certain assumptions about how your code is laid out. Of course, these conventions can be overridden to support legacy database tables, old code or developer whims. CakePHP uses camel case for naming models. Models are usually also singular. Some examples of model names could be:


The models in CakePHP assume that the corresponding database is named using the plural form and underscores. The above example models would be associated with database tables named:


Controllers typically include “Controller” in their class name and the plural form of the model with which they are associated. Using the previous examples our controllers would be:


File names use underscores between words. The files for controllers include the name “controller”. So our controllers would be found in the following files:


When naming your models and controllers, simple is often best. The controller name by default is used to create the URL so keeping controller names simple and descriptive will keep your site URL structure simple. A final name convention deals with views. Views are associated with a method of a controller class and are named after the controller and method separated by an underscore. For example:


CakePHP’s Directory Structure

CakePHP’s directory structure helps organize your application by separating the code that is part of the framework from your own code. This makes upgrading the framework simpler. Here is the typical directory layout for a CakePHP application. We’ll discuss what each directory should hold.

/<main directory>

The /cake subdirectory is where the CakePHP code itself resides. This allows you to easily upgrade only the core CakePHP framework. Likewise, the /vendors subdirectory allows you to separate vendor specific libraries to simplify upgrading any dependent libraries. The /app subdirectory is where you will actually write your code for your application. The /webroot directory is where you will serve static files such as your CSS stylesheets, images and JavaScripts.

Installing the Framework

To get started with CakePHP, you will first need to download the framework from the CakePHP web site. Once you’ve downloaded the framework, unzip the archive. You’ll end up with a folder containing the directory structure we talked about earlier. Copy this into the directory where your CakePHP application will reside. The archive includes a number of .htaccess files so you’ll want to make sure those get copied over as well. Next, make sure that the /app/tmp subdirectory is writable by the user account that your web server runs under. CakePHP uses this subdirectory for caching and other temporary storage so if it is not writable, you will get an error.

Configuring Apache

The next step is to configure Apache to properly serve your CakePHP applications. To get pretty URLs you will need to have Apache’s rewrite module loaded and enabled. Obviously, you also need to have PHP enabled. In your Apache configuration, you will need to set your web server’s DocumentRoot to the /app/webroot directory of your CakePHP install. The default Apache configuration, defines some pretty restrictive permissions for directories so you’ll need to allow the .htaccess files that shipped with CakePHP to override your Apache configuration. These files contain the rewrite rules necessary for Apache’s rewrite module to generate pretty URLs. These settings are set on the directory where you installed CakePHP. Here are the settings you’ll want:

<Directory /full/path/to/your/CakePHP/installation >
	Options FollowSymLinks
	Allow Overrides All
	Order allow,deny
	Allow from All

You’ll also need to make sure that Apache is configured to run files with the .php ending as PHP scripts. If you browse to your site now, you’ll get the CakePHP welcome screen. You’ll see a couple of error messages as we have not yet configured our database or CakePHP’s settings.

Creating and Configuring a Database

To really get the most out of CakePHP, you’ll want to create a database. To create a database called “demo”, connect to MySQL and issue the following command:

create database demo;

You should not work as the MySQL root user so you’ll also want to create a special database user and give it permissions for the demo database. You can do this all in one with the following command:

GRANT ALL on demo.* to 'demouser'@'localhost' 

Now you’re ready to configure CakePHP to use your new database. There is a sample database configuration file named database-sample.php in your /app/config directory. Your actual database configuration should be called simply database.php and must reside in /app/config. Your database.php file should include this:

	var $default = array (
		'driver' => 'mysql',
		'persistent' => 'false',
		'host' => 'localhost',
		'login' => 'demouser',
		'password' => '<password>',
		'database' => 'demo',
		'prefix' => '',

A couple of quick notes on this part. The parameters for connecting to the database are pretty self-explanatory. The ‘prefix’ variable is used if you want your CakePHP tables to begin with a prefix. This is often used when support legacy applications that may have used prefixes to the table names. The persistent option specifies whether you want to use persistent connections to MySQL. Finally, the variable $default indicates that this is the default database connection to use for our models. You can also define a $test array as the database you want to use for unit testing. If you application will connect to more than one database, you can define additional variables with those database connection parameters. This will allow you to specify to which database your models should connect.

Production vs. Development: Configuring CakePHP

The final step is to configure CakePHP itself. There are a few settings that are required whether your environment is a production or development system. Other settings are more likely to be selected based on whether you are configuring a development system or a production application.

You will find the settings being discussed in /app/config/core.php This file controls the core behavior of CakePHP. The first settings you will need to update are Security.salt and Security.cipherSeed. Security.salt is a random string of numbers. Security.cipherSeed should be a random string. I use 46 characters for my cipherSeed. If you do not update these to new values, your default CakePHP page will indicate an error.

Two other related security settings include Session.timeout and Security.level. The Session.timeout value indicates the number of minutes before a session should expire. The default is 120 minutes or 2 hours. However, the Security.level affects this value. A ‘High’ security level means that the session will timeout in Session.timeout x 10 minutes. ‘Medium’ multiplies the timeout by 100 and ‘Low’ multiplies it by 300.

Speaking of sessions, you may want to change the session setting in core.php. The default setting is to use PHP based sessions. Database sessions can reduce performance slightly but if you plan on scaling your application to multiple servers or performing any kind of load balancing, database sessions are far more scalable. You can set CakePHP to use database sessions by setting the setting to ‘database’. You’ll need to run the cake shell command:

cake schema run create Sessions

to create the database schema for sessions. You should also check the setting for Session.model. This setting defines the model that CakePHP will use for database driven sessions. The default value is the “Session”.

CakePHP’s debug level determines how much of an error message CakePHP prints to the display. For production servers, this should be set to 0. Development systems can use 1 or 2. A setting of 1 will display warnings and errors. A setting of 2 will also display SQL code and full debug messages. If your development server is publicly accessible, this may not be a safe practice. In addition to the debug level, CakePHP also features a log level. This provides some granularity so that in a production environment you can turn off the debugging but still have errors and debug messages written to your logs.

This should get your CakePHP framework up and ready. Now you’re all set to begin developing with CakePHP.

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
Don't miss another post! Receive updates via email!
  1. Manoj says:


    Thanks a ton! I was able to configure & use CakePHP