CakePHP Tutorial

CakePHP is a great PHP framework which we admire. It is secure, smart and relatively fast. In the following CakePHP tutorial we will cover some interesting points.

Supported Databases

CakePHP is very flexible in regards to databases support. Currently version 1.3 supports:
  • mysql - MySQL 4 & 5,
  • mysqli - MySQL 4 & 5 Improved Interface (PHP5 only),
  • sqlite - SQLite (PHP5 only),
  • postgres - PostgreSQL 7 and higher,
  • mssql - Microsoft SQL Server 2000 and higher,
  • db2 - IBM DB2, Cloudscape, and Apache Derby (
  • oracle - Oracle 8 and higher
  • firebird - Firebird/Interbase
  • sybase - Sybase ASE
  • adodb-[drivername] - ADOdb interface wrapper (see below),
  • odbc - ODBC DBO driver

For all the database options please check the file app/config/database.php and the configuration array inside the class DATABASE_CONFIG.

Log actions in CakePHP

Sometimes it may be helpful to log what visitors are doing on your site, especially for your website security. For this purpose you can use CakePHP default logging system. In your controller simply add:
$this->log("Someone is here. Possibly security issue.", 'debug');
Then you will see the message in the file app/tmp/logs/debug.log

Furthermore, you may also include the visitor's IP address by using CakepPHP Request Handling component.

Disable Mod_rewrite

By default CakePHP comes with pretty urls enabled. That's great unless your web server does not support mod_rewrite directly or you have previous .htaccess rules breaking the current one.

If mod_rewrite does not work properly the main css will not load, neither any links will work. Luckily, it is easy to disable it by opening the file app/config/core.php and UNcommenting the following line:
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
After that everything should look and work perfect. Your links will be such as:


Setting variables in controllers for use in views

In the MVC model you cannot just define a variable in a controller and expect it to be available in the corresponding view. Instead it has to be set by 'set' as follows: In controller:
$total = $this->Example->find('count'); //this will find out all the examples we have
$this->set('total', $total);
In view:
echo $total; //only after being set you can use it.

Send form intput to controller

Most of form operations such as edit and save can happen automatically. However, if you'd like to have a custom form send input to a controller you might wonder how to access the GET_ or POST_ variable. This variable is available to the controller through the variable $this->data['MODEL']['FORM_FIELD']. Here is an example:
echo $form->create('Site', array('action' => 'list_sites'));
echo $form->input('filter', array('label' => 'Filter domain'));
echo $form->end('Filter');
The above form is for the Site model and will submit data to the list_sites method inside the SitesController. There it will be available such as $this->data['Site']['filter']. Here is how my whole method looks like:
public function list_sites($filter = null) {
        if ($this->data['Site']['filter']) {
            $this->paginate = array(
                'conditions' => array('Site.domain LIKE' => "%" .
				$this->data['Site']['filter'] . "%"),
                'limit' => 10
        $data = $this->paginate('Site');
        $this->set('data', $data);
        $total = $this->Site->find('count');
        $this->set('total', $total);
The best thing is that you don't have to sanitize the input from the form because it is automatically done. Perfect for website security.

CakePHP Themes and Styling

If you'd like to change the look of your CakePHP site there are a few things to do:
  • Create a directory for your new theme inside app/views/themed/. For example, if we'd like to use a theme called security we will create the directory: app/views/themed/security/
  • To make the controller use the security theme please specify inside of it:
        public $theme = 'security'; 
  • The next thing is to put in app/views/themed/security/ your layouts. In short, layout is the HTML structure of your pages. In layouts you implement menu position, backgrounds and so on. The latter cannot be set in your views files but only in the layouts. You will need at least one layout but you can use a different layout for each of your controller methods. If not explicitly specified CakePHP will look for the default layout called default.ctp. A simple layout will look like this: 
        <title><?php echo $title_for_layout; ?></title>
        <meta charset="utf-8"> 
        <?php echo $this--->Html->css('908634.css'); ?>
        <div id="pagewidth">
            <div id="wrapper" class="clearfix">
                <div id="maincol">
        echo $content_for_layout;
                <div id="leftcol"> <h2>Menu</h2>

  • You will notice a few variables above. First $title_for_layout is set in each controller method as usually:
    $this->set('title_for_layout', 'List sitez');
  • Next there is the style to be used - 908634.css. This file has to be created in the directory: app/webroot/theme/security/css/ There is nothing peculiar for the style - it is a standard css file.
  • In the layout file you will also find the variable $content_for_layout. This variable is generated by your views and their code goes there.
  • Last but not least - don't forget that your views files now will go into app/views/themed/security/ and not just in app/views/

blog comments powered by Disqus