CakePHP 1.3, Why you should upgrade

I have been doing a lot of work on the relatively new CakePHP 1.3 release. According to the bakery the finial version is just around the corner so i decided to take the time and go over a few of the new and interesting features introduced in CakePHP 1.3.

Extend your routes!

When i’m working on cakephp apps i make use of a custom class which gathers all the of the page slugs and connects them up through the router, allowing you to for example visit the page with the name “about-us” by going to “http://example.com/about-us”. This wasn’t the greatest solution, especially for larger websites resulting in a lot of routes being generated.

With the new CakeRoute class making custom routes has never been so easy. In fact mark story wrote a great blog post detailing the creation of a custom route doing exactly what i was doing but without all that junky code.

Javascript helper no longer needed

There has been quite a few changes made to the html and javascript helpers, one the nice changes is the movement of javascript::link() to the html helper as html::script(), not to mention its cousin script blocks has also made the move. With these changes why do you even need to load the javascript helper? Well its depreciated along with the Ajax helper and have been moved into…

The new Js Helper

Cakephp 1.3 brings to the table the new JsHelper. The JsHelper the successor to the Ajax helper with a few new tricks up its sleeve. Not only are there a few new functions added it is now framework independent. What this means is you don’t have to be using prototype to take advantage of this helper. From right now the Js helper supports jQuery, Moo Tools and Prototype out of the box with support to add more by creating your own engine.

Plugins! now with less suck

Around the time cakephp 1.3 alpha came out i was starting a project that was going to rely heavily on plugins and after looking through the 1.2 documentation i realised it was simply not possible. With all the new work done in 1.3 plugins now can do almost anything that you can do within your app folder.
Some of the changes include:

  • The vendors folder has been removed and the webroot folder (assets) have been introduced
  • Schema generated via the cakephp shell can now be generated for a specific plugin.
  • Datasources can now be bundled (wait it wasn’t there before?)
  • Configuration can be loaded using Configure::load(’plugin.file‘);

Form wide options!

One of my favourite new changes is the fact you can now include options within your form::create() function and those options will then apply to all the form inputs within that form. Lets say i was in a situation where i need to change all the div tags to p tags? Well it i had to do it like so:

<?php
echo $form->create('Page');
	echo $form->input('name',array('div' => array('tag' => 'p')));
	echo $form->input('slug',array('div' => array('tag' => 'p')));
	echo $form->input('heading',array('div' => array('tag' => 'p')));
	echo $form->input('body',array('div' => array('tag' => 'p')));
echo $form->end('Save');
?>

Well thats no way to do views, here is the new and improved way:

<?php
echo $form->create('Page',array('div' => array('tag' => 'p')));
	echo $form->input('name');
	echo $form->input('slug');
	echo $form->input('heading');
	echo $form->input('body');
echo $form->end('Save');
?>

Thats much better!

Libs

The easy explanation of libs is its vendors but its files you use internally either within your app or within your organisation. This allows you put such stuff as your routing classes or code that you write that doesn’t deserve the light of day. Even though this isn’t much of a new feature i found it nice to be able to separate all of my 1st and 3rd party code from within my apps.

Before Filter just lost some weight

Anyone who uses the auth component a bit will know that their is a bit of configuration, Now you can set your setting for core components within to components array as so:

<?php
var $components = array(
	'Cookie' => array(
		'name' => 'MyCookie'
	),
	'Auth' => array(
		'userModel' => 'MyUser',
		'loginAction' => array('controller' => 'users', 'action' => 'login')
	)
);
?>

As we all know before_filter is very sensitive about its weight so every bit helps :)

Conclusion

By no means is this everything CakePHP 1.3 has to offer and if you still interested i encourage you to checkout the cookbook for a more comprehensive list of whats changed.

Tags: ,

Wednesday, March 17th, 2010 CakePHP
  • Slowly but surely I am being lured over to CakePHP and changes like these are making me very keen.
    The promises of Symfony 2 seem quite lofty and am thinking that maybe wont live up to as promised? But one thing I would like in cake is support for doctrine. A more focused ORM done on its own can mean that more effort spent on the framework itself can go to other areas like php5 only - what happened to Cake 2.0 (hey isn't 6 around the corner) dependency injection blah blah blah to bring us up closer to the enterprise guys... But anyway it is looking good, thanks for the work!
  • markbeukers
    I personally think that the benchmarks symphony 2 are touting are a little off, no doubt it will be fast (for php) but ill believe their claims when its out of alpha.

    Regarding Cake 2, As far as i know effort will be shifted to 2.0 not long after 1.3 has gone gold and the api will have minimal changes, ensuring a relatively quick release.
  • A great functional summary of some of the great changes that are going through the RC steps now with CakePHP. Nice work Mark.
  • markbeukers
    Thanks for the praise, I hope that i can bring some value to the table with this blog and not just ramble about nothing.
blog comments powered by Disqus