• PHP
  • 3 MINUTES READ

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

  • POSTED ON
  • June 23, 2015
  • POSTED BY
  • Aayan Arif
  • POSTED ON June 23, 2015
  • POSTED BY Aayan Arif

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....

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

CakePHP2webroot.htaccess

to

myCake1Projectwebroot.htaccess

and replace it with the older one.
11. Copy

cake2webrootindex.php

to

sitewebroot

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 cakeapp folder and compare folder names and change it in your particular siteapp
15. Backup core.php file and replace it with

cakeappconfigcore.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

cakeappConfig

to

siteappConfig

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

cakeappConfig

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

cakeappConfig routes.php
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';

19. Rename bootstrap.php file and

copy

cakeappConfigbootstap.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. If you are looking for php web developer for your business that can help you to elevate your business. visit us today

ABOUT THE AUTHOR

Aayan Arif

Content Strategist at vteams - Aayan has over 8 years of experience of working with multiple industries.

0 Comments

Leave a Reply

More Related Article
We provide tips and advice on delivering excellent customer service, engaging your customers, and building a customer-centric business.