• PHP
  • 4 MINUTES READ

Migrating CakePHP Sites to PHP7

  • POSTED ON
  • May 13, 2016
  • POSTED BY
  • Aayan Arif
  • POSTED ON May 13, 2016
  • POSTED BY Aayan Arif

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

Rapid developments in the CakePHP framework enable e-commerce websites to work in a structured and rapid manner. For a much smoother transition, migration of CakePHP7 to PHP7 became necessary. 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 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 the 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 the steps mentioned below:

  • Set the PHP path; For example C:xamppphp. Add cd to the app and then run the Consolecake command to upgrade all.
  • Copy cake2webroot.htaccess to sitewebroot.htaccess and replace it with the older one.
  • Similarly, copy cake2webrootindex.php to sitewebroot 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 are to be written as product_categories.
  • Take a backup of core.php and replace it with cakeappconfigcore.php. After that, update your site Settings; For example, set debug value in order to change cookie name;
    Configure::write('Session.cookie', 'MYAPP'); will now be
    Configure::write('Session', array(
           'defaults' => 'php',
           'cookie' > 'MYAPP'
                          ));

    In the same way, you can also copy security salt and cipher seed values.

  • Copy database.php default from cakeappConfig to siteappConfig and rename it to database.php.  Now, you can set values from your backup.
  • Rename routes.php and copy routes.php from cakeappConfig to your site folder. Paste the following two lines to your current routes.php at the end of the file;
    CakePlugin::routes();
    require CAKE . 'Config' . DS . 'routes.php';
  • Also, rename bootstrap.php before copying cakeappConfigbootstap.php to your site folder. Then copy all your constants and variables defined in the old bootstrap.php to this fresh bootstrap.php file at the end.
  • Open AppController to modify Auth component settings. For example:
    var $components = array('Session', 'Acl','RequestHandler','MobileDetection',
      'Auth' => array(
               'loginAction' => array(
                       'controller' => 'users',
                       'action' => 'login'
               ) ,
               'logoutRedirect' => array(
                       'controller' => 'users',
                       'action' => 'login'
               ) ,
               'authError' => 'You have logged out',
               'authenticate' => array(
                       'Form' => array(
                               'fields' => array(
                                         'username' => 'email'
                               ) ,
                               'scope' => array(
                                         'User.active' => 1,
                                         'User.is_deleted' => 0
                               )
                       )
               ) ,
               'authorize' => array(
                              'Actions' => array(
                                         'actionPath' => 'controllers/'
                              )
               ) ,
               'unauthorizedRedirect' => array(
                              'controller' => 'landings',
                              'action' => 'home'
               )
      )
    );

    Update the render function for the 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:

    function  __construct($request = null, $response = null) {
            parent::__construct($request, $response );
            //your rest of the stuff
        }
  • Modify your components to make sure that they are extending the Component class instead of Object. In addition, initialize method that accepts controller object, i.e.:
    function initialize(Controller $controller){
               $this->controller = $controller;
  • AppController, AppModel, and Apphelder should be in their respective Controller directories with the 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 the users controller:
    if($this->request->is('post') && $this->Auth->login()
    ) {//rest of the code
    }
  • 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 the 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 an 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 encapsulate 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 support 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.

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.