I wasn’t using CakePHP since past 6-8 months. I was a big fan of the framework but it seemed heavy to me. Among other issues, the most crucial factor was building and loading model associations. The process required time and was still high on resource consumption. Few days back, I came back to see how the CakePHP is doing with its version 2.
It was good to find the lazy loading of model associations as the first feature of CakePHP 2.0 official documentation. This regenerated my interest in CakePHP and I started exploring more. The post might look outdated because CakePHP 2.0 was released on 16th Oct 2011 and CakePHP 2.1 on 23rd Jan 2012. But, after having worked on CakePHP for more than 2 years, I could not stop myself from writing about the new features of the framework. With CakePHP 2.0, they have dropped support for PHP 4 and have refactored the library code to make it strictly complaint with PHP 5.2+. Modeled on Ruby on Rails (RoR), CakePHP is a tough competition to Zend framework, Symfony and CodeIgniter.
Lazy Loading in CakePHP 2
Lazy Loading means fast speed. In the earlier versions of CakePHP, all the model associations, helpers and components were loaded by default in the bootstrap. This adversely affected the performance in big projects having large number of models and helpers. With almost everything lazy-loaded in CakePHP 2.x, you will see a lightning fast response even in the debug mode. The associated models, helpers and components are loaded on-demand.
The models will no longer need database connection for construction/loading of associations.
New CakeRequest and CakeResponse Model
The CakeRequest collects information about a request. In the earlier versions, the request features were spread across
RequestHandler, Router, Dispatcher and Controller. There was no universal Object for everything related to request. CakeRequest is the new object that takes some responsibilities of RequestHandler component. Yet, it is powerful than the RequestHandler. The CakeRequest centralizes the GET, POST and FILES arrays into Cake’s data structures. It also provides the header information like domain and application server related information. The
params property is deprecated and
$this->request is the recommended way for accessing the request object.
The routing parameters like
webroot, etc. are also moved to CakeRequest.
To top of the features, the CakeRequest allows you to extend the request detectors by using
Similar to CakeRequest, the CakeResponse brings together all the response handling features into one object. With CakeResponse methods, you can interact with the response. Few of the available methods are
CakeEmail Library for sending mails
You don’t have to worry about integrating third party libraries for sending mails with CakePHP. The CakeEmail library of CakePHP 2.x makes it simple to send emails. You don’t have to send email from Controller. It is a library now and you don’t have to use it as a Component chained to a Controller.
App::uses('CakeEmail', 'Network/Email'); … $email = new CakeEmail(); $email->from('email@example.com')->to('firstname.lastname@example.org')->subject('CakePHP 2 is here')->send('Read about the features of CakePHP 2.');
Class Loader – File and Folder Naming
Adopting the PHP standards, CakePHP team reworked the way the files and folders are structured and named. Important changes related to file names are:
- All files containing classes should be named after the class it contains.
- Extending first convention, no file should contain more than one class.
So, older controller, helper and component naming conventions change.
A controller file name, my_first_controller.php becomes MyFirstController.php
A helper file name, cache.php becomes CacheHelper.php
While, a cookie component file name, cookie.php becomes CookieComponent.php
Folder naming convention depends on the contents of the folder. If a folder contains classes, then it should be CamelCased. The folders that don’t contain classes should be lowercased. So,
controller folder becomes
view/helper folder becomes
tmp folder names don’t change because these folders don’t contain any classes and thus, do not constitute a namespace on themselves.
The biggest change in Class Loader is that the
App::import() is replaced by
App::uses(). This method accepts two parameters. The first one is the name of the class that you want to load and the second is the package name or namespace to which the class belongs.
// Old: App::import('Controller', 'Users'); App::uses('UsersController', 'Controller'); // Old: App::import('Component', 'Cookie'); App::uses('CookieComponent', 'Controller/Component'); // Old: App::import('Core', 'Sanitize'); App::uses('Sanitize', 'Utility');
App::uses() doesn’t recursively locate the classes and fetch the classes from the defined package. This leads to dramatic improvement in the performance.
Next post on this blog will cover other important changes in CakePHP 2.0 and 2.1 (beta).