How To Upgrade From CakePHP 1.x to CakePHP 2.x

  • Post published:June 23, 2015

A new version 3 of CakePHP has been released. If you are still running CakePHP 1.x then you should consider upgrading your application to its latest version ASAP! CakePHP 1.3 is deprecated and no longer being maintained. CakePHP 2.x contains many improvements to every layer of CakePHP including:

  • Caching
  • Request Handling
  • Response output
  • PDO Support
  • PHPUnit Integration

CakePHP 2.x also supports PHP Version 5.2.8 and above as mostly servers now have PHP Version 5 or above. Most of the things are lazy-loaded in CakePHP 2.x. One can have a look at the list of changes and class loading with application.

You need to follow the following steps while upgrading your CakePHP project from CakePHP 1.x to CakePHP 2.x.x:

1. Backup your project for disaster recovery
2. Download CakePHP 2.x.x from cake downloads
3. Extract CakePHP 2.x.x in a folder CakePHP2
4. myCake1Project is the folder name of CakePHP 1.x project. Copy the CakePHP2/cakephp/lib folder to myCake1Project/lib folder
5. Change directory to /app
6. Make sure that you are able to run Console/cake command
7. Now at first, you need to Dry Run command mentioned below so that you get to know what it will do with your project:

./Console/cake upgrade all –dry-run

8. Make sure that script/user logged in has the rights to create/delete/rename folder files
9. If you are satisfied with the dry run, you can go ahead and run

./Console/cake

upgrade all command. It will list down all the changes made
10. Copy

CakePHP2\webroot\.htaccess

to

myCake1Project\webroot\.htaccess

and replace it with the older one.
11. Copy

cake2\webroot\index.php

to

site\webroot

and replace it with the older one
12. Modify login function in users controller, now you will have to implicitly call login() function:

if($this->request->is('post') && $this->Auth->login() ){
//rest of the code
}

13. Replace request parameters like

$this->request->params['form']['remember_me']

with

$this->request->data['remember_me']

and

$this->request->params['url']['remember_me']

with

$this->request->query['remember_me'];

14. Rename the folder, upgrade shell usually skips many folders. It only renames those having underscore in name like product_categories to ProductCategories. But rename of all folders must be done otherwise it will create problem on Linux. For example, vendors should be Vendor, just open cake\app folder and compare folder names and change it in your particular site\app
15. Backup core.php file and replace it with

cake\app\config\core.php.

After that, update your site settings, for example change cookie name, set debug value etc.

Configure::write('Session.cookie', 'MySiteName'); will now be  like:
Configure::write('Session', array(
'defaults' => 'php',
'cookie' > 'MySiteName'
));

16. In the same way, copy security salt and cipher seed values
17. Backup database.php file and

copy database.php.default from

cake\app\Config

to

site\app\Config

and rename it to database.php. Open it and set values from your backup file
18. Rename routes.php file and copy routes.php from

cake\app\Config

to your site folder, open it and copy routes settings from your backup, otherwise you can just paste the following two lines in your current routes.php file (in the end of file) instead of replacing it with

cake\app\Config routes.php
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';

19. Rename bootstrap.php file and

copy

cake\app\Config\bootstap.php

to your site folder, then copy all your constants and variables etc defined in old bootstrap.php to this fresh bootstrap.php file (in the end of file)
20. In case, you are using a constructor, update it now as it also accepts cake Request and Response objects as a parameter. For example:

function __construct($request = null, $response = null) {
parent::__construct($request, $response );
//your rest of the stuff
}

(NOTE: This change should also be done in other controllers as well where you have constructors)
21.

$html->script()

is replaced by

$this->Html->script()

22. $form is replaced by

$this->Form,

for example:

$form->create();

is replaced by

$this->Form->create();

23. $session is replaced by

$this->session

If you have followed each and every step correctly, then you would not confront any issue while upgrading your application from cakephp 1.x to 2.x.