Stanford University Wiki

CakePHP (most popular PHP framework)

229pages on
this wiki

What: CakePHP - most popular PHP framework Edit

What is: CakePHP, a web framework Edit

CakePHP is a web framework built in php. A web framework is a concept that has been developed from the understanding that most web applications - at their core – are quite similar. A web frame work provides a foundation of components which web applications can be built on. With a web frame work in place – and preferably out of sight - developers can devote more time to their site’s core functionality.

Overview: Cooperation built on a common foundation Edit

The features within CakePHP are those that are common to nearly all web applications. By coming up with best practices to structure a web application, less time has to be spent determining the best code layout and educating new members of a team. Listed below are the most important facilities of CakePHP:

Humanize the database Edit

SQL’s INSERT, SELECT, UPDATE, and DELETE and the accompanying syntax could benefit from an upgrade. These statements correspond to the most common web app database operations: create, read, update, delete (often abbreviated CRUD). Logically, CakePHP provides a CRUD abstraction using the ActiveRecord design pattern. The ActiveRecord approach wraps the database table and row in a php class. This way, developers do not have to switch from thinking about php to SQL when writing their applications... By Dhivakar.hggtgrS. PHP Code Examples

MVCg architecture Edit

After writing a “quick” web app by mixing together html, php, and SQL in the same file it is quickly apparent that the code is difficult to understand and not maintainable. By making different parts of a web application modular and loosely connected, parts can be swapped in and out or changed quickly. This is what MVC is. It is a way to make the main mechanisms of a web application – the model, the view, and the controller - separate from each other. This way a designer, who would be in charge of the views, can be isolated from the details of the database (model) or business logic (controller). I will go into more depth about MVC in the example.

Application scaffolding Edit

About 80% of the time, a field in a form corresponds directly to a field in a database. Before scaffolding, developers would have to write code that would make an input field in a web form, verify what was inputted into the web form matches the type the database is expecting, create a new row in the database to store the data, and then store the data. With scaffolding, this process is done automatically. The developer defines what they want in the database and the framework does the rest. Scaffolding is implemented in CakePHP by restricting the name of components. By doing this CakePHP is able to determine relationships between the components of the system. I will go more in depth on the subject of naming in the example..

Code generation Edit

Code generation is like bringing scaffolding to the foreground. What this means is that instead of having boilerplate code automatically written behind the scenes, code generation will bring it to the foreground so that it can more easily be modified. Overall, I find this feature and scaffolding complement each other quite nicely. Scaffolding primary use case is to get the code up and running quickly. Code generation allows for the scaffolding to be replaced and or customized- if need be.

Data validation Edit

Data validation can be considered a subset of scaffolding. Data validation is an important feature that reveals a broader concept within the CakePHP framework. That concept is that everything should only need to be defined once. So, if in the model, the developer states they are expecting a zip code, the data validater will read in from the model what it is expecting and return an error to the user if they enter something that is not a zip code before the data begins to propagate through the business logic and into the database.Edit

Authentication and Access control lists Edit

Almost all applications require a username and password. Therefore CakePHP provides an easy mechanism to implement this functionality. Behind the scenes each user account also has attached to it a level of access they have to different data in the site. So, for example, a site administrator has access to all the data and can edit everything. A user, on the other hand, might only be able to read some data and write information to their own account profile. CakePHP provides a way of making groups of users in the authentication system. Additionally, decorators are added to the Models to make them only viewable by certain groups.

Flexible Caching Edit

Using a framework is not computationally free. In fact, a frame work is more computationally intensive then, say, serving static web pages. A way to reduce this computational tax on the server is by caching "expensive" database queries, thereby reducing the load on the server. Various parts of the CakePHP framework can be cached easily.

Data sanitization Edit

Just like food needs to be cleaned before we eat it, data has to be cleaned before it is passed into a web application. Data can be dirty in the sense a small percentage of users will try to enter in data that will try and break the application. Cleansing data is a difficult process because there are so many edge cases and number of possible inputs and expected outputs. By adding this feature into the framework, applications are safer, more maintainable, and easier to develop.

Localization and internationalization Edit

Localize your web application to different languages is a fast and relatively easy way to get more users. CakePHP provides a way to localize an application by creating a file of key-value pairs. An application can then set its language based on the url that access it (ex. or Within the application the keys are used to refer to certain strings. The localized values are then displayed to the user depending on the language setting.

Documentation, Examples, and User Community Edit

The value of documentation should never be undervalued. CakePHP's well built out documentation and examples are a critical component of the framework. They provide a place for people to go to learn by example and see what other people are building and how they are building it.

Some of the items may seem mundane, but, in fact, those are the most important! These mundane features are the ones that take up the most time to develop and are the most commonly needed by developers.

Example Edit

Blog Edit

In this example we will recreate a blog using the CakePHP framework.

Home screen for the blog.

First we have to define a table of the database which will be used by the model. Naming is important in CakePHP. The framework interprets the connections between items based on how they are named.

We create the table in the database called posts as follows:

   title VARCHAR(50),
   body TEXT,

Then we create a Model that will wrap around this table

     class Post extends AppModel {
       var $name = 'Post';

Now we are able to do the CRUD operations on the model. Notice the naming of the model Post and the naming of the table in the database Posts. The model is singular because it will contain one post. The table will be used as a repository to store many Post objects. CakePHP is able to determine the singular and plural forms of the two items and know to connect them. By connecting them, the framework is able to create the scaffold which we will use later. This is a broader example of how some predefined structure in a framework is able to make developing web applications easier.

Next we create the controller. The controller will put and get data from the model as well as do any processing of the data.

   class PostsController extends AppController {
     var $name = 'Posts';
     function index() {

This particular controller above will be “activated” when someone goes to the URL


The scaffolding allows the framework automatically write some of the code to update blog posts.

The set method sets the variable “posts” with the data in the database. The posts variable is then made available to the view where the data can be displayed. This is great modular design for code. This way, the backend of the system can be changed without affecting the frontend of the system. I would also like to point out how easily it is to access the database without using SQL: this->Post->find('all') returns all the post in the database.

Now to display the information that was in the posts variable to the user we write the view component of the MVC as follows:

     Blog posts

Id Title Created


<?php echo $post['Post']['id']; ?> <?php echo $html->link($post['Post']['title'], "/posts/view/".$post['Post']['id']); ?> <?php echo $post['Post']['created']; ?>

         <?php endforeach; ?>

We have now built the basics of a blog. CakePHP has far more functionality. This was just your first slice of cake. If you are interested, I would encourage you to learn more.

Critique Edit

Strengths Edit

Speed Edit

PHP was developed to be a fast language. The language has been around for a long time and there are many speed optimizations that are built into the language. In time other languages will speed up as well, but, at present, php is the fastest.

Scalability Edit

By having more control over the database and SQL queries, developers are able to have tighter control over how the application will scale. Although, I would hypothesize, most users are more interested in producing a first version quickly, and then worry about scaling, if needed.

Free/Runs on most web hosts Edit

The framework is completely open source. Almost all web hosting companies will have php installed. Whereas, some web hosts do not have python/Django/Pylons pre-installed.

Weaknesses Edit

Database not tightly coupled to framework Edit

Another feature that I found missing in CakePHP was the ability to automatically create tables based on the definitions of the models. By having to define the models and the tables, I had to enter the same information twice. Django solves this problem by automatically creating tables based on the models. Ruby on Rails has an even tighter integration with the database. Ruby on Rails is able to update the database schema based on new information, a feature that CakePHP lacks.

No admin interface Edit

One feature that I found missing from the CakePHP framework that Django, a Python web framework, has is an administrator interface. All web applications require some level of human maintenance. If there are going to be multiple administrators for a site, it is great to have a web interface to do the administration as opposed to having to do it directly on the database. With Django, most of the administrator interface is prebuilt and requires no extra work by the developer.

Uses for CakePHP Edit

CakePHP, being a framework, can be used for most any type of web application. Possible scenarios when a developer would not want to use a framework would be if they are running a very performance oriented site and the frameworks overhead was too high for the volume of pages they are serving. Another reason for not using CakePHP would be is the learning the framework was harder than simply making the webpage from scratch or using already existing code.

Conclusion Edit

When a developer is deciding which framework to use, I would hypothesize what they are really doing is looking for a framework in a language that they know best. The difference in features between modern, well rounded frameworks, are small in comparison to what language the framework is written in. With this assumption, CakePHP would be an excellent choice of a framework for someone who has already done development in php. However, if a developer is coming in with a clean slate, I would recommend using Django. Django, in comparison to Ruby on Rails is faster but less easy to use. Similarly, CakePHP is faster than Django, and less easy to use. Therefore, Django strikes a good balance between ease of use and performance. Additionally, Python has many more modules written for it than php and the Django community is more active than the CakePHP community.

Reference: CakePHP cookbook

Around Wikia's network

Random Wiki