Migrating from CakePHP 1.3 to 2.0
I love CakePHP. It’s a PHP framework with a clear and strict Model / View / Controller (MVC) separation and a great abstraction layer for reading, updating and saving objects to the database. Recently, the CakePHP team released CakePHP 2.0, and I started updating my projects to this new version. Since it was my first big version jump with the framework, and a lot of substantial changes were made compared to the 1.3.x branch , I ran into some quibbles, which I thought would be nice to document here.
Getting started
There are several ways to update, but for small to medium projects, I found it best to just unzip the new CakePHP 2 structure, and manually copy over your Controllers and Views to their new folders. Mind you that the folder structure has changed significantly. For instance, the controllers map is now Controller. Pay attention to where you copy the files – don’t worry about the filenames themselves, we’ll come to that later.
Make sure you update core.php and database.php (now located in (cakephp root)/app/Config) with the values for your application. Make sure to copy over the salt and cipherseed values from your old installation too, since CakePHP will continue whining when you use the default ones – it’s a security risk. Also, to get the database connection up and running again, the syntax was changed:
'driver' => 'mysqli'
becomes
'datasource' => 'Database/Mysql'
Bulk work: Upgrade via console
In order to rename all your app files to the new CakePHP file structure, there is a console command. New to CakePHP 2.0 is that each application now has its own console. Navigate to your app’s console (not the Console in /lib/Cake !) in /app/Console, make sure the Console command is executable by doing a chmod +x on it, and execute:
./cake upgrade all
This will rename all files to the new cake standard, and update all references within your PHP code.
Cleaning Up
Unfortunately, this is not the end. Chances are thin that your code will just run fine now. Have a look at the CakePHP 2.0 migration guide for more information. I’ll sum up the issues I’ve dealt with the most here:
- In your views, you now have to address the Helper classes through the $this object. No more calling, for example $html->link( , it’s $this->Html->link( now, sir.
- The JavascriptHelper is now called JsHelper.
- The Auth component has changed significantly. The login() action is not automatically added when you use the component, you’ve got to manually specify it now. Check the new Auth component documentation for more info.
- The AjaxHelper has been removed, along with the handy functions for AJAX-style searches, like observefield. I’ve cooked my own observefield using jQuery – use at your own risk. I serialise a form, which in this case holds a query input box which allows me to do a live-search-and-update for Reservations :
$this->Js->get('#query')->event('keyup', $this->Js->request( array('controller' => 'sales','action' => 'searchReservations', $event['Event']['id']), array( 'update' => '#view', 'async' => true, 'dataExpression' => true, 'method' => 'post', 'data' => $this->Js->serializeForm(array('isForm' => false, 'inline' => true)))