- CakePHP evolves to 2.0
- Other changes in CakePHP 2
Today, I am continuing with the features of CakePHP 2.0. We already read about the Lazy loading, CakeRequest and CakeResponse models, CakeEmail and PSR-0 Standard Compliance. In a separate post, we will also try to explore these standards.
In today’s post, we will explore other changes and improvements that CakePHP team has made in the version 2 of the popular PHP framework. Modifying the core libraries to meet certain custom requirements was often a tricky situation. Aliasing removed the need to modify the core Cake library.
Aliasing Class Names
CakePHP 2 adds support for aliasing helpers, components and behaviors. In earlier versions of Cake, you must have seen aliasing in Cake Models. The extension of aliasing to helper, components and behaviors allows you to use your class instead of a different one. The alias will be available throughout the application. If you alias the HTML helper, then any element or helper that uses HTML helper will use the aliased class.
// Example of Helper Aliasing public $helpers = array( 'Html' => array( 'className' => 'RefHtml' ) );
Having aliased the HTML helper, you will be able to use RefHtml class instead of Html. You won’t be required to make any changes in the elements. This means, you can still continue to use $this->Html in your view files. Similarly, the calls to the Paginator Component will call RefPaginator component.
// Example of Component Aliasing public $components = array( 'Paginator' => array( 'className' => 'RefPaginator' ) ); // Example of Behavior Aliasing public $actAs = array( 'Containable' => array( 'className' => 'RefContainable' ) );
Now you don’t have to modify the core files.
PHPUnit Testing Framework
As per the official release notes, CakePHP team has “Dropped SimpleUnit in favor of PHPUnit.” PHPUnit is the de-facto standard for unit testing PHP projects. In addition to the features of PHPUnit, CakePHP offers extensions to add new features for even easier testing.
PHPUnit can be installed through the PEAR Installer. Jenkins can also be integrated with CakePHP to automate the running of test cases. Jenkins is a continuous integration server.
The test cases can also be executed from commandline.
Use of Native Exceptions, PHP Data Objects and Standard PHP Library
CakePHP 1.x had certain in-built features to wrap around PHP functions. PHP has improved and CakePHP team decided to use the native PHP features and remove useless overheads. For example, the duplicate code for json_encode is removed and Cake is starting to rely on the functionality shipped with PHP.
CakeError class has been removed and the native Exceptions functionality is being trusted for indicating errors in code. To better handle the exceptions, custom handlers can be created for each exception.
PHP Data Objects extension defines a consistent interface for accessing databases in PHP. CakePHP 2 uses PDO for handling database connections. Switching the database engine will be lot easier now.
The Standard PHP library provides excellent support for file and folder operations.
Authentication Refactored
Cake provides 3 authentication methods: FormAuthenticate, BasicAuthenticate and DigestAuthenticate. The Basic and Digestion authentication methods moved from SecurityComponent to AuthComponent. The Cake no longer automatically hashes all passwords. Only DigestAuthenticate hashes the sensitive information like passwords. Custom authentication objects can be used.
Session Handling
Session Handling has been made easier. CakeSession is a static class and all its methods are called statically. Some duplicate code is removed from Session class as well. CakePHP allows you to handle sessions with database, cache or any custom session handlers. The active(), activate() and __start() methods are removed.
HtmlHelper and FormHelper
The HtmlHelper has been enhanced by adding the getCrumbList() method and moving loadConfig() method from Helper() to HtmlHelper() class.
The FormHelper supports HTML5 form inputs. By defining the type as email, you can create an HTML email input element.
Among other changes:
- There is JavascriptHelper with support for jQuery, Prototype and Mootools libraries. It can be extended to add any other library.
- Debugging is improved by replacing HTML output with distinguishable blocks.
- The console support is improved with restructured console.
- Order of operations changed in the views. beforeLayout() and beforeRender() fires before the evaluation of scripts and content.
- Simplified structure for rewriting URLs. Arrays and nested arrays can be passed to URL.
- Cache is now an abstract static class that cannot be directly instantiated.
- A number of Constants are removed. This includes APP_PATH, CONFIGS, CONTROLLERS, LIBS, MODELS, VIEWS, etc.
- Changes are made to the router methods as well. For example, Router::setRequestInfo() cannot be modified and Router::connectNamed() should be used instead.
- Dispatcher is moved to Cake/libs. And the Dispatcher::dispatch() now takes two parameters. i.e. CakeRequest and CakeResponse instances. The methdos like baseUrl, getUrl, uri have been removed as well.
- The Xml class is refactored.
- I18n functions have been improved for improved multi-lingual support.
- Improved the support for SqlServer, PostgreSql and SQLite databases.
Some of the changes incorporated in CakePHP 2.1 beta are:
- Various model methods like saveAll, saveAssociated now support passing the fieldList for multiple models.
- New media method of HtmlHelper can be used to generate HTML5’s audio and video elements.
- Other changes are related to Cake Views. ThemeView and XmlView are modified while View Blocks and JsonView are added.
- Conditional View rendering saves CPU cycles and reduces memory utilization.
You will see other changes and improvements when you start using CakePHP 2.x. I am sure that you will love the Cake.
CakePHP is licensed under the MIT license and is available to download from the CakePHP web site.
Follow Us