Migrating from CakePHP 1.3 to CakePHP 2.0
With the recent release of CakePHP 2.0 Beta, I decided to upgrade ActionTracks to CakePHP 2.0. I thought this would be a popular topic so I am adding this article to document the process. The Cake console has and upgrade utility that will handle several of these tasks for you. I had already started the process when I discovered this utility. I did use it to do some find and replaces on things like helpers. While the utility is a great addition, some people may not be comfortable automating that much. While there are a lot of very cool new features in CakePHP 2.0, which I will be covering in my overview, this article is focused on what you need to do to get your CakePHP 1.3 application upgraded to CakePHP 2.0.
Installing CakePHP 2.0
This is pretty straight forward. Install Cake per the instructions. There isn't much difference between installing CakePHP 2.0 and previous versions. Unzip the downloaded file in your desired location.
You should start with default core.php file. Update the configuration values to match those in the app you are migrating. Make sure you copy the Security.salt and Security.cipher_seed from your old core.php or you won't be able to login in addition to other possible security issues. Copy any custom configuration values you have added to your old core.php. Take notice of how errors, sessions, and caching has changed.
The database configuration has only a minor change in how you designate the database engine. Previously, you used this:
'driver' => 'mysqli',
now, you use this:
'datasource' => 'Database/Mysql',
The Route file has been changes slightly. Cake's default routes are now optionally loaded at the end of the file. If you do not want to use Cake's routes, then you can comment this line. In addition, CakePHP 2.0 now has much better handling of plugins. One of those features is automatically loading routes for plugins. You can place your custom routes before or after
CakePlugin::routes(), depending on whether you want your custom routes to be matched before or after plugin routes.
Other than loading plugins, the bootstrap file hasn't changed much. Copy any functions, constants, or other code from your old bootstrap into the new one.
The directory structure for CakePHP 2.0 is a bit different from previous versions. Most notably, most files and folders are now CamelCased instead of lower_underscored. My understanding is that this was done so that files and directory names matched their class names. This should also provide a performance boost as file and folder names will not have to be run through the Inflector Class. As a result, you will either need to rebake your files and then copy code from your old files. While this might be the best bet if you are using a lot of baked controllers and views; for most, this would create a lot of work. Instead, I decided to simply copy all of my old files into their respective directories in the new app. I then went through and renamed the models, controllers, and component files to match the class name. Your users controller called users_controller.php becomes UsersController.php. While you need to rename the View folders to be CamelCased, the view files keep their naming conventions so that they match the action name.
Move App Files
The parent app_ classes have been moved and renamed as well. Instead of app_controller.php, app_model.php, and app_helper.php now become Controller/AppController.php, Model/AppModel.php, and View/Helper/AppHelper.php.
Copy Email Elements
Email elements have been moved from views/elements to View/Emails.
Ok, now let's make things work! At this point, I was actually getting a login screen. I couldn't login however. I disabled the Auth Component temporarily to see what worked. Surprisingly, most things were functioning and rendering ok.
The Auth Component has gone through quite a bit of changes and now boasts some very cool features. Auth now supports multiple authorization adapters that can all be used together. No longer are you limited to using actions, controllers, or crud as the sole method of authorization. Not only can you create custom adapters, but you can also use multiple adapters together. This will be incredibly powerful! As a result of these new features, the configuration of the Auth Component has changed. You will need to update this:
$this->Auth->authorize = 'actions';
$this->Auth->actionPath = 'controllers/';
$this->Auth->flashElement = 'flash/error';
$this->Auth->authorize = array(
'Actions' => array(
'actionPath' => 'controllers'
$this->Auth->flash = array(
'element' => 'flash/error',
'key' => 'auth',
'params' => array()
CakePHP 2.0 has consolidated some request related classes into the Request Class. This changes how you get data from forms and passed from other methods.
The Request Object does not have form element any longer. You will need to replace
$this->request->data. In addition,
$this->data needs to be replaced with
$helper->function(). You need to update your helper calls to use
$this->Helper->function(). I did run the utility to update the helper calls. I did notice, however, that it did not update the Paginator calls. I had to do that with a custom find and replace.
The __() function no longer has the option to echo. It always returns. As a result, any calls that used the return argument need to be updated. This is often used by the Paginator when rendering the counter as well as Session flash messages. Change this:
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%')
CakePHP 2.0 has replaced SimpleTest with PHPUnit. Unfortunately, this is where I have had the greatest trouble. I initially tried installing PHPUnit from pear, however, I was running into errors that turned out to be related to MAMP's default pear configuration. Deleting the pear config file ended up solving that problem. I decided to rebake my tests and then copy the test methods from my old app. I am having trouble with some fixtures that use virtual fields on my Model tests. I can't seem to get any controller tests to run. Once I solve the testing dilemmas, I will come back and update this.
I still have a few CSS issues to resolve, but the application is up and running! Now I can't wait to dig into the new features!