Try Tuts+ Premium, Get Cash Back!
Cake PHP

Getting Started With CakePHP

CakePHP is an open-source framework for PHP intended to make developing, deploying and maintaining applications much easier. CakePHP offers many useful design patterns, such as the Model-View-Controller pattern, seen in other popular frameworks like Ruby On Rails. The CakePHP framework also provides a slew of valuable reusable libraries for dealing with common tasks. An example is “Inflector” (a routine that takes a string and handles pluralization).

This guide will attempt to point you in the right direction so you can get started with CakePHP and eventually develop your own applications using it.

Step 1: Why CakePHP?

A significant amount of development time with PHP is spent rewriting common code for routine operations such as database access or returning data to the browser. Of course, all this routine code can quickly become disorganized in traditional PHP applications. What is needed is a framework for PHP that does what Ruby On Rails did for Ruby.

CakePHP has been around for awhile and does exactly that. It provides a number of useful libraries in support of common tasks and includes facilities for organizing code in folders and associating code with files. As a result, time spent writing and organizing code becomes greatly reduced.

Below are just a few things CakePHP offers to make development easier.

  • Free Open-Source MIT License allowing you to use CakePHP applications within any of your own projects.
  • Compatibility with both PHP4 and PHP5. The minimum version needed is 4.3.2.
  • Support for MySQL, PostgreSQL SQLite, PEAR-DB and wrappers for ADODB, a database abstraction library.
  • Model-View-Controller layout.
  • Easy CRUD (Create, Read, Update and Delete) database interaction.
  • Scaffolding to save production time.
  • Search Engine Friendly URLS.
  • Input validation and sanitization tools to make your applications much more secure.
  • Templating with familiar PHP syntax.
  • Caching Operations.

Step 2: Download The Framework

Before you start developing with CakePHP you will need your own copy of the framework uploaded to your server. Visit CakePHP.org and click the large “Download” button. Make sure to download the stable release and not the release candidate. There are also many different file formats available so you can pick the best one for your computer.

Step 3: Uploading and Understanding the File Structure

Once you have your fresh copy of CakePHP out of the oven, the next step is to upload the copy to a PHP and MySQL enabled web space. I would recommend creating a new directory for CakePHP projects.

Once the upload has finished the directory structure should look something like this:

/path_to_root_folder
	/cake/
	/docs/
	/app/
    
    		config/
		controllers/
		models/
		plugins/
		tmp/
		vendors/
		views/
		webroot/
		index.php
		.htaccess
        
	/vendors/
	index.php
	.htaccess

All these directories or folders may look a little daunting at first, but the separate directories are meant to better organize all of the framework components. Since names like “tmp” aren’t self-explanatory here is what these folders are for:

  • The cake folder stores all the core functions and internals for CakePHP. You will usually not need to edit anything here.
  • The docs folder contains very little, but does hold the license information (COPYING.txt), a change log and some other useful files. This directory is not important for CakePHP to run so you can remove it if you wish.
  • The app folder is where your application code will go. The app folder will hold your controllers, configuration, templates and much more.
    • The config folder contains all the configuration files for the application. This includes database details, access list, inflections and routes (URL rewriting).
    • The models folder stores all the sql database functionality for your application.
    • The views folder stores all the templates, layouts (header, footer) and helper modules that assist functionality (such as AJAX).
    • The controllers folder stores all the controllers for your application. A controller is the part of the application that directs and controls the model and the views by accepting input and deciding what to do with it.
    • The plugins folder stores plugins which are a combination of models, views and controllers that can be packaged and used in other applications. Examples are user management modules or an RSS module.
    • The tmp folder stores cache files generated by the caching system and also stores debugging logs. This folder will be very valuable during development.
    • The vendors folder, can contain other libraries that you want to include in a particular application.
    • The webroot folder stores static media such as CSS, images and the JavaScript needed by your application.
  • The second vendors directory will allow you to store third-party libraries and hook into them from your CakePHP controllers. For example if we wanted to built a Facebook application with CakePHP we could drop in the Facebook library and configure CakePHP to load it.

Step 4: Configuring CakePHP

Configuring CakePHP is pretty straightforward. We just need to tell CakePHP our database details and set up how we want the core functionality to work.

For development purposes you should create a new database and a user with the following privileges: ALTER, CREATE TEMPORARY TABLES, CREATE, DELETE, DROP, SELECT, INSERT, UPDATE, REFERENCES, INDEX, LOCK TABLES.

Once the user and database have been created, we can find CakePHP’s database configuration file, located in /app/config/database.php.default

Open and scroll down to the following array

var $default = array('driver' => 'mysql',
			'connect' => 'mysql_connect',
			'host' => 'localhost',
			'login' => 'user',
			'password' => 'password',
		        'database' => 'project_name',
			'prefix' => '');

and fill in your database details as necessary. If for some reason you cannot create a new database, or your host does not allow it, you can set a table prefix for all your CakePHP tables by setting a value in the ‘prefix’ index. Make sure to rename this file to /app/config/database.php

More core configuration is located in /app/config/core.php. You can change the level of debugging information, how sessions are stored, session time outs for security, and the names of cookies. Once we start developing we may need to adjust these, but the defaults are fine for most needs.

Step 5: Making Sure it Works

Once you have entered the correct database details and uploaded all the CakePHP files, the installation should be ready for development. Point your browser to the folder that you uploaded the installation to. If everything is working, you should see the following success page:

Closing

This tutorial was meant to introduce the basics of CakePHP and how to get it up and running on your server. In future installments we will look at developing an application from the ground up using CakePHP, adding effects such as AJAX, and integrating CakePHP with other libraries and services such as Facebook.

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Tags: cakePHP
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Anu

    i did as u told and u got this error… Fatal error: Call to undefined method SplFileObject::getBaseName() in E:\www\cakephp-cakephp\cake\libs\cache\file.php on line 282 any idea whats wrong?

  • http://www.alyanschi.com alyanschi

    thank you, but I was expecting some further detailed information :)

  • http://www.moretablet.com MT-Francis

    Thanks..We’ll try this one later..

  • Ashish Saxena

    Great Help For New Bie

  • huevo

    Thanks for the introduction!!

  • http://webmastersintexas.com Jerry Lee

    Great tutorial. However, it seems like every tutorial out there covers how to setup an MVC, or even create the “Blog App”, but no one seems to cover building a website from ground up with an MVC. I know it may be a big tutorial, but it would really help a lot of us wanting to use the MVC architecture. I have been handcoding websites for years, and usually with the php extension, but just with the basic header, footer, sidebar, setup. I am intrigued with the idea of an MVC for my larger sites. Can you consider doing a complete tutorial covering a dynamic website? Thanks!

  • rahul

    Thanks it was useful

  • Pingback: 20 Collection CakePHP Tutorials You Should Know | Daily Syntax

  • kundan kumar

    Thanks you provide good stuff for beginner of CAKEPHP

  • http://www.webdesignergeeks.com Ajay

    Nice tutorial for beginners.
    i am read it first time when i hands on CAKEphp.
    Nice explanation and easy to understand thanks :)

  • madhurendra

    Thanks to the Good article.

  • http://toppersbbs.dtdns.net/mapnet1 Joe

    What do I do when I setup every thing like this said, and load the index page. And it’s telling me that there files that it can’t find. Like in

    cake/boostrap.php, and the folder and file are not in the zip file. I started a new project in netbeams and added CakePHP to it. and I setup my database.php file. and I loaded the page like it said. and this is what I get, Files not fount.

    There are other errors like this. It points to a file called index.php on the webroot folder…

    Does any one know where I can get help with this.

    Joe

  • http://techiesblog.in/ Ashok

    Very Nice article for Beginner

  • LI HOLEN

    Cakephp 2.1.1 is installed in a sub folder on my site – mysite.com/cakephp.

    I get this error: URL rewriting is not properly configured on your server.

    welcome page looks a little funny (no images or css styles)

    What files is to be changed and were are they placed in the directory??

    • Damian

      Hi, that happens when you dont have the apache mod-rewrite module active. Just make sure you uncomment the loadmodule mod_rewrite in the apache httpd.conf file.

      Mine in OSX looks like this:

      LoadModule rewrite_module libexec/apache2/mod_rewrite.so

      Regards,
      Damian

  • http://packersandmoverschennai24.in/ sathish

    it ‘s super thanks for sharing

  • Chan Myae

    Thanks for sharing ur knowledge and waiting for the additional ….

  • fahim

    great stuff

  • ahmed

    nice work ….thanks alot

  • shahab

    thank you … it was very useful

  • http://www.php2utor.com php2utor

    actually i was trying with cakePHP 2.1 . I am always getting error

    Error: ProductsController could not be found.

    Error: Create the class ProductsController below in file: app\Controller\ProductsController.php

  • shahbaz

    i have created 3 users in cakephp with the help of acl techiniques.Admin is working all works(like crud).but manager and user is not working..means not login……….

  • rajeev

    thanks

  • Medhat Dawoud

    great article .. thanks

  • mmt

    what is programming difference between core and cake php tell me

  • tet

    rsadsdaasd

  • tet

    dhfosd fjs

  • tet

    ertertdrtet

    • php developer

      its a good tut

  • tet

    sadfsdf

  • tetsdfsdf

    sdfsd

  • phpcodelance
  • hbb

    hhgmg

  • ghj

    hjkhjkhjkhj