Below is a translation of the article by Jonathan Snook on the advantages and disadvantages of two popular PHP frameworks CakePHP and CodeIgniter .Before publishing this kind of publication, I usually fear for all kinds of fanatics who react inadequately to such comparisons. Therefore, I try to approach the story as objectively as possible. All conclusions in the article are made only on the basis of facts.
I compare these two platforms against each other, but there is no unconditional winner here. Both have both strengths and weaknesses, as well as complete failures in one or another option, which may be necessary for you.
Why compare?
CakePHP and CodeIgniter are in many ways very similar to each other, including the fact that both support PHP4. Speaking about the advantages of one platform, we can immediately refer to the strengths of the other.
')
They both make it possible to work in the MVC architecture paradigm, which simply means their ability to separate the Model (data), the Controller (which sends data from the model to get an idea) and View (what the user sees).
They both use routing, which makes it possible to bind the URL to a specific function inside the controller (in CakePHP, this is called actions). CodeIgniter supports regular expressions for routing, while you have to wait for CakePHP version 1.2 to support this feature.
Amendment: CakePHP 1.1 already supports regular expressions for routing, but this is not reflected in the documentation.They both support scaffolding, which helps to automatically generate a View on the basis of the Model. In CodeIgniter scaffolding is simplified and allows access to similar functionality by adding a keyword to the URL. I consider this a disadvantage, because I often develop personal projects not for public use, and this keyword is annoying.
Let's start ...
Approach to simplicity
I am convinced that CodeIgniter attracts due to its simplicity. Most of the work is to create a controller, load libraries, get data from the Model, and send the result to View. Everything is very clear and you can see how it works.
Simplicity in CakePHP is achieved through automation (“automagic”). This makes the coding process fast, but without studying the kernel, you will simply ask yourself the question “what is happening now?” As for me, I prefer to understand how everything works. Beginner developers this aspect simply discourages.
Work with models
The operation of the Model in CodeIgniter is very clear and will allow the use of standard SQL in combination with some commands as in this example:
$query = $this->db->getwhere('mytable', array(id => $id), $limit, $offset);
$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);
$query = $this->db->get();
Note: The chaining method used in the second part of the example can only work in PHP5.You can also create a model object, load it and, using various methods, perform certain tasks. If desired, this is best done at the model level, rather than the controller, so that the code remains within the scope of MVC.
CakePHP has a slightly different approach, which is to automatically load the model corresponding to this controller (controllers and models that have the same name are interconnected). You can disable this autoloader and even specify on your own any models that need to be loaded into the desired controller.
In addition, CakePHP has in its arsenal a mechanism to help establish relationships between models, which helps greatly facilitate the extraction of data. For example, while in the post_controller controller, you can do the following:
$this->Post->Comment->findAllByPostId($id)
I chose this example because it helps demonstrate two different concepts. First, you can see how I get access to the comment model through the post model (thanks to the established association in the post model). Secondly, thanks to the
findAllByPostId
method. CakePHP allows you to retrieve records from the database via
findByX and
findAllByX queries, where
X is the equivalent of the field name from which the selection is made.
I think Cake is a good demonstrator of one of its best sides in its ability to automatically load associated data. Take a look here:
$this->Post->findById($id)
This request will give us not only the publication, but also comments to it. Pretty practical.
Validation
When working with models, you will inevitably have to deal with data validation. In CodeIgniter, this functionality is represented by the validation class. With it, a set of rules is declared and is attached to the verification object. The validation object automatically (I assume) validates data from a URL or form. Of course, here you can determine how it will happen. The validation class can also assist in the process of defining error messages for a particular field.
In CakePHP, validation is done directly through the model in two ways. The first method verifies the entire model, and the second - each field separately and gives us the callback
beforeSave
for certain fields.
CakePHP version 1.2 will include a redesigned validation system to increase flexibility.
Kinds
CakePHP immediately includes a default template and has two variables:
title_for_layout
and
content_for_layout
. Any action is automatically linked to a specific view (again “automagic”). And as long as you call your files in a special way, the controllers will be automatically linked by models and views. Accordingly, getting around all this, and defining your own templates and view files is not a problem. Unfortunately, there is no convenient way to get the generated view code, and hence the problems with the implementation of the caching mechanism.
With CodeIgniter, the approach is simpler and almost comes down to the usual include'ing. Each file is loaded and processed. There is also a built-in templating class, but it is not simpler than the built-in view handler. You can do in the manner of CakePHP by constantly including the header and footer in the template, but this will only be an external similarity. CodeIgniter allows you to replace the view and caching mechanisms with third-party or custom template engines.
(And yet Smarty is perfectly embedded in both CodeIgniter and CakePHP. - comment of translator)Built-in features
CodeIgniter, at my discretion, here unconditionally wins thanks to such classes as FTP, Email, File Uploading, XMLRPC, Zip encoding, and many others.
CakePHP in turn boasts an extensive repository (
the Bakery ). You can easily, as in CodeIgniter, connect any necessary classes. What is interesting: although I did not try to do this, there is an opportunity to connect many classes written for CI to CakePHP.
Autoload
CakePHP allows you to make extensive changes to applications by modifying the base controller. You can also create global methods using the model file, which later will not prevent you from making edits at the controller level using callbacks such as
beforeFilter
,
afterFilter
and
beforeRender
. Similarly, helpers and components can be loaded at the level of an individual controller.
CodeIgniter can load helpers, libraries and plugins, but it does it globally for the entire application.
(Here the author is mistaken, because in CI at the controller level the following construction is available: $ this-> load-> whatever () - comment of the translator.)Documentation
Documentation is the key to understanding any framework in order to develop on it.
CodeIgniter has a complete documented list of all components with each of their methods and parameters. CI has forums and wikis with many custom code fragments.
CakePHP in turn is not as well organized. The manual is outdated and does not disclose what the API offers us. In addition to the format of the original documentation, you can get the offline version in CHM or PDF. But still, CakePHP has Bakery, with custom articles and components. The development team is very helpful through IRC channels (#cakephp at irc.freenode.net). And also quite active
Google group .
At last
I am quite a pragmatic person and sincerely believe that both of the frameworks presented have a great future. They help to approach the development of projects more accessible means than it is implemented, for example, using
Symfony .
I personally remain a more CakePHP fan than CodeIgniter for its “automagic”. And this advantage will more clearly reveal itself from version to version.
Refinement
This comparison was based on the documentation for CodeIgniter 1.5.2 and experience using CakePHP 1.1. I intend not to touch on the issue of performance, because It is quite problematic to prepare and test each option.