R&D

Migrating CakePHP Sites to PHP7

Rapid developments in CakePHP framework enable the e-commerce websites to work in a structured and rapid manner. For a much smoother transition, migration of CakePHP7 to PHP7 became a necessity. Thus, after a series of incompatible versions, one official support came out in CakePHP 2.8 version that showed full consistency with the PHP7. This article is going to list down steps for migrating CakePHP websites to a more flexible and easy-to-use framework – PHP7.

There are two popular versions of CakePHP in the current market:

  • 2.x.x: This version is more commonly used for developing a web application. However, it needs to be migrated to CakePHP 2.8 version at first before migrating to PHP7.
  • 1.x.x: This version of CakePHP is outdated due to which web applications developed in this version needs to be migrated to PHP7. The migration is divided into the following two steps:
    • Migrate 1.x.x to 2.5 version
    • Migrate 2.5 to 2.8 version

Steps to Migrate 1.x.x to CakePHP 2.5 Version

The migration from 1.x.x to CakePHP 2.5 version can be done with the help of steps mentioned below:

  • Set the PHP path; For example C:\xampp\php. Add cd to app and then run Console\cake command to upgrade all.
  • Copy cake2\webroot\.htaccess to site\webroot\.htaccess and replace it with the older one.
  • Similarly, copy cake2\webroot\index.php to site\webroot and  replace it with the older one.
  • Make sure to rename all the folders with underscores in between to avoid any hassle on Linux; For example productcategories is to be written as product_categories.
  • Take backup of core.php and replace it with cake\app\config\core.php. After that, update your site Settings; For example, set debug value in order to change cookie name;
    In the same way, you can also copy security salt and cipher seed values.
  • Copy database.php default from cake\app\Config to site\app\Config and rename it to database.php.  Now, you can set values from your backup.
  • Rename routes.php and copy routes.php from cake\app\Config to your site folder. Paste the following two lines to your current routes.php at the end of file;
  • Also, rename bootstrap.php before copying cake\app\Config\bootstap.php to your site folder. Then copy all your constants and variables defined in old bootstrap.php to this fresh bootstrap.php file in the end.
  • Open AppController to modify Auth component settings. For example:
    Update render function for mobile detection case and update it. If you are using constructor, you can now accept cake Request and Response objects as a parameter. For example:
  • Modify your components to make sure that they are extending Component class instead of Object. In addition, initialize method that accepts controller object, i.e.:
  • AppController, AppModel and Apphelder should be in their respective Controller directories with proper renaming of the folders for your own convenience. It is highly recommended to use APP . ‘Vendor’ . DS for importing vendors. You can also use App::uses instead of App:import for Cake’s internal libraries.
  • Modify the login function to implicitly call it in users controller:
  • Check the security component again to ensure its accuracy and modify email sending. Now, replace  $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’]; for RememberMe component.

Steps to Migrate 2.5 to CakePHP 2.8 Version

The migration from 2.5 version of CakePHP to its 2.8 version can be done as follows:

  • The action option on FormHelper::create() is deprecated, which means that the action key of an array URL is now being respected for the generation of DOM ID.
  • CakePHP now will adjust the memory limit to 4MB in order to avoid any fatal errors. If you want to disable the feature for other purposes, change Error.extraFatalErrorMemory to 0 in your Config/core.php file.
  • The addition of Cache::add() will enable you to add data if the key does not exist. This method also works in:
    • Memcached
    • Memcache
    • APC
    • Redis
  • CakeTime::listTimezones() argument has been changed to accept array in the Cache argument. Valid values for the $options argument are:
    • Group
    • Abbr
    • Before
    • After
  • Helper Classes can be created with console applications that encapsulates re-usable blocks of output logic.
  • On enabling a new option i.e. “no-locations“, the generation of location references in your POT files will be disabled.
  • Hash::sort() is supporting case-insensitive sorting via the ignoreCase option.
  • Magic finders also successfully supports custom finder types and can use IN and NOT IN operator in conditions to find them. For example; you can use findPublishedBy and findPublishedByAuthorId functions through magic method interface.
  • Validation::uploadedFile() has been back-ported from 3.x.
  • The Cake Session cacheLimiter configuration is now added to help in defining the Control Headers used for the session cookie.
  • For tags to be created without HTMLaction attribute, ‘url’ => false has been created to support FormHelper::create() function.