Intro to CakePHP

CakePHP is an Open Source development framework, built largely on PHP, but also utilizing tools like Ajax for some visual flair. Primarily, though, it’s a Model-View-Controller (MVC) Framework, modeled after Ruby on Rails.

What this means, essentially, is that it is a nice way of structuring the code so that different activities are separated logically. Database interaction and data validation should always be done in the Model. Displaying of data should always be done in the View. And any special logic that needs to be done to link the data to a display should be done in the Controller.

These are essentially extensions of Object-Oriented Programming. In fact, the representations of each Model and Controller are actually Objects, which can be extended or inherited.

CakePHP has it’s little quirks with these implementations that are meant to make development faster, but can trip up a developer new to Cake if they are unaware of them. CakePHP has very specific expected naming conventions.

A Model, for example, might be called Event. The file representing this model will need to be placed in the app/models directory and named event.php. The Class name (a Class being a logical block of code, and an Object being an instance of that Class) would then need to be Event. The related database table would also need to be named events. Notice the difference in capitalization. If the file name is not lower case and/or the class name does not begin with an uppercase letter, the Model may not work properly, or might require extra coding to make it work, which can get unnecessarily tedious.

Typically, each Model should have a matching Controller that performs any necessary activities between the Model and the Views. This Controller should be placed in a file in the app/controllers directory and named events_controller.php. The Controller Class should be named EventsController. Notice the difference in pluralization. If these naming conventions are set properly, no extra coding is required to link the Model to the Controller. Also notice how multiple words are handled: all lowercase and separated by an underscore for file names, and differentiated with capital letters in the class name. This would work if you wanted a Silly Photos MVC set: app/models/silly_photo.php for the SillyPhoto Model and silly_photos database table, and app/controllers/silly_photos_controller.php for the SillyPhotosController class.

Views are a little different in that they are related directly to the Controller. Generally, the names of View files will match exactly with the method names in the Controller. Usual methods for a Controller are add, delete, edit, and index. The related View files for the EventsController would be located in app/views/events/ and would be named add.ctp, delete.ctp, edit.ctp, and index.ctp, respectively. The subdirectory of the views files must match the Controller’s file name, minus the _controller text (ie: views for SillyPhotosController would be located in app/views/silly_photos/). The case and pluralization of the Controller method names and View file names should be the same, so a method called list_items should have a View file called list_items.ctp.

There are exceptions to these rules that can be made, but it’s generally easier to stick with these conventions. The only time you might need to explore the exceptions is when trying to integrate code that is maintained by a third party, where their conventions do not quite match those in CakePHP.

In my next post I will describe some of the performance and configuration gotchas I have experienced while learning and developing with CakePHP.

Leave a Reply