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 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:
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.
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post