Fabulous PHP frameworks: CakePHP


Become An Insider

Sign up now and get free access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content from the best tech brands on the Internet: CIO, CSO, Computerworld, InfoWorld, IT World and Network World Learn more.

CakePHP combines easy installation with command-line tools that jump-start development, striking a good balance between small and large

CakePHP, a product of the Cake Foundation, successfully walks the line between large frameworks (such as Zend) and small frameworks (such as Lithium). CakePHP's documentation claims that the framework requires no configuration. The CakePHP website gushes, "Set up the database and let the magic begin." This is a bit colorful, but not too far from the truth. The framework also features excellent command-line tools that dramatically reduce the time required to build an application. The current stable release, Version 1.2.9, requires at least PHP 4.3.2. 

As with other frameworks, CakePHP defines a strict directory structure, with equally strict rules regarding which files go in which subdirectory. This is referred to as "convention over configuration" and is in keeping with the framework's "no configuration" claims. You don't modify a configuration file to specify the location and function of files, classes, or methods; rather, you place specific elements in predetermined locations within the project. For example, all the library code is kept in the /cake subfolder of an application directory, and documentation expressly states that you must swear an oath not to modify the files in that directory. The directory structure also distinguishes between first-party libraries and third-party libraries, which helps you keep your corporate libraries separate from those acquired from the outside.

The convention over configuration principle carries through to framework entity names. Model classnames must be singular and camel-cased. For example, a model that manages music albums might be represented by a class named MusicAlbum. Table names corresponding to CakePHP models are plural and underscored, so our table for the MusicAlbum model would be music_albums. CakePHP even provides a utility library for pluralizing names to assist in the process.

CakePHP Cake and Bake
CakePHP has a command-line script utility -- called Cake -- that runs "shells," or a script for building or maintaining a CakePHP application. For example, CakePHP provides a built-in shell called Bake. If you've already defined the database for your application, the Bake shell will create model classes and method scaffolding for that database. If requested, it will even construct unit tests for the model. Should you wish to create your own shells, documentation describes the process. Once you've written a shell, store it in a specific directory, and it is immediately available through the Cake script.

One of CakePHP's more important view helpers is the AjaxHelper, which employs the Prototype and script.aculo.us JavaScript libraries to simplify the use of AJAX in an application's view system. You pass into the AjaxHelper an associative array that specifies behavior such as whether AJAX calls are synchronous or asynchronous, which elements are passed to the server, which DOM elements are updated by the returned information, and so on. The helper reads this array and manages all the behind-the-scenes drudge work for you. It also supports callbacks that allow you to hook into the AJAX process before, during, or after the actual AJAX call is made.

Both models and controllers support callbacks as well, allowing your application to hook into CakePHP's processing at specific points. Thus, attaching a callback to a model using CakePHP's beforeSave() call allows your application to intercept processing just before data is saved to the database (which you might employ to encrypt the data before it's stored).

Up from the database
As suggested by CakePHP's motto, application development typically begins with the database. Create a database and tables, establish a connection to the database in your CakePHP application (one of the rare times that you actually configure something in CakePHP), run the Cake script to build your model access files, and when you open your application's home page, CakePHP displays a welcome page that not only verifies that you've set the system up properly, but also provides recommendations for securing the website. For example, it will recommend that you change the security salt string -- used for generating cipher hashes -- to something other than the default value. From this starting point, you build your application outward, adding controllers and views.

CakePHP's documentation consists of an online CookBook, which is a combination reference guide and tutorial. Actually, the CookBook provides two tutorials; one leads you through creating a simple CakePHP application, the other concentrates on authentication and security features. An API reference is also online and lets you browse the framework by class, file, or package. The CakePHP site maintains documentation for the three most recent releases, which is a nice feature if your application was written in one of the framework's earlier versions. If you're a visual learner, the website's community section hosts CakePHP TV, a small collection of videos covering various CakePHP development topics.

To continue reading, please begin the free registration process or sign in to your Insider account by entering your email address:
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
You Might Like
Join the discussion
Be the first to comment on this article. Our Commenting Policies