SlideShare a Scribd company logo
How to migrate CakePHP 1.x to 2.x

Keyword: CakePHP development, CakePHP 2.0, CakePHP migration

We will focus on what we need to do to get CakePHP 1.3 application upgraded to CakePHP 2.2.3
CakePHP 2.2.3 is the latest stable version now.

Installation
   •   This is pretty straight forward, same as CakePHP 1.3 version.
   •   Download and unzip the CalePHP 2.2.3 and follow the instructions.

Configuration
   •   core.php
             •      Make sure to copy the security.salt and Security.cipher_seed from your
             old core.php
             •      Take notice of how errors, sessions and caching have changed.
   •   database.php
             •      There is a minor change on this file,
             •      CakePHP 1.3: 'driver' => 'mysqli'
             •      CakePHP 2.2.3: 'datasource' => 'Database/Mysql'
   •   routes.php
             •      Don't overwrite this file with your older one.
             •      You can place your custom routes before or after CakePlugin::routes()
   •   bootstrap.php
             •      Copy all functions, constants and other code from your old bootstrap into the
             new one.

Folder Structure
   •   The cake folder is now inside lib folder. There is nothing to do with this.
   •   Files and folders are now CamelCased instead of lower_underscored
              •       Example: The “users” folder inside the View becomes “Users”
   •   The controller files are now UsersController.php instead of users_controller.php
              •       project_user_controller.php becomes “ProjectUsersController.php”
   •   The model files are now User.php instead of user.php
              •       project_user.php model becomes “ProjectUser.php”
   •   The components files are now FormatComponent.php instead of format.php
       •      The helpers files are now DatetimeHelper.php instead of datetime.php

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


Auth Component and Login

   •   Auth now supports multiple authorization adapters that can all be used together
   •   Cake 1.3.x was automatically checking if user has correctly entered username/password
       inside your login() method of users_controller but in cake 2.x we need to manually call
       $this->Auth->login() this returns boolean value based on successful login or failure.
If you are using “email” as your login field name
      CakePHP 1.3: (inside AppController beforeFilter)

         $this->Auth->fields = array('username' => 'email', 'password' =>
         'password');

      CakePHP 2.2.3: (inside AppController beforeFilter)

         $this->Auth->authenticate = array('Form' => array('fields' =>
         array('username' => 'email', 'password' => 'password')));

      Auth Login
      CakePHP 2.2.3: (inside UsersController login function)

             if (!empty($this->request->data)) {
                      if ($this->Auth->login()) {
                           $this->redirect($this->Auth->redirect());
                      } else {
                           //$this->Session->setFlash('Your Email or Password was
             incorrect.');
                      }
                  }

      •     CakePHP 2.x auth automatically hashes the password on login but not on
      save.

         We can add a beforeSave() method on the User model to hash the password.
         public function beforeSave($options = array())
         {
           $this->data['User']['password'] = AuthComponent::password($this-
             >data['User']['password']); return true;
         }

Request Data
  •   CakePHP 2.0 has done some request related changes.
  •   The Request Object does not have “form” element any longer.
  •   You will need to replace $this->params['form'] with $this->request['data'] or $this-
      >request->data.
  •   $this->data needs to be replaced with $this->request->data
  •   So, now we can use $this->request->data on both form submit and AJAX post.
  •   Now, we need to check !empty($this->request->data) instead of “!empty($this-
      >data)” while saving a form.


Views Changes
  •   Use $this->Html->link() instead of $html->link()
  •   Use $this->Form-> instead of $form->
  •   Use $this->Session-> instead of $session->
  •   Use $this->Paginator-> intead of $paginator ->
  •   For JavaScript inclusion use $this->Html->script(“”) instead of $javascript->link()

Moved Email Elements
  •   Email elements have been moved from views/elements to View/Emails.
Helpers
  •   The Ajax, Javascript, and XML helpers have been removed. You will need to replace these
      helper calls with appropriate alternatives
  •   Helpers can no longer be called with “$helper->function()”. You need to update your helper
      calls to use $this->Helper->function()
  •   If you are loading or importing your helpers inside another custom helper or component,
              •      $myhelper = new DatetimeHelper() becomes $myhelper = new
              DatetimeHelper($this->_View) or $myhelper = new DatetimeHelper(new
              View(null))

Components
  •   All component files should be extended by Component Class instead of Object



DB and Code Caution
  •   There is no Enum Support in cakephp2.x as sql server doesnt have similar datatype.
             •      You can change enum to tinyint(2)
  •   In cake 1.3 used tinyint(1) for is_active database fields.
             •      While retrieveing from database it returns 0/1
            •      But, cakePHP2.x no longer returns as 0. This will return bool(true)
            /bool(false)
            •      Boolean column values will be casted to php native boolean type
            automatically.

__() Function and Pagination
  •   The __() function no longer has the option to echo. It always returns
  •   Change the Pagination code,

  •   CakePHP 1.3:
            •      echo $this->Paginator->counter(array( 'format'             => __('Page %page%
            of %pages%, showing %current% records out of %count%              total, starting on
            record %start%, ending on %end%'', true)));
  •   CakePHP 2.2.3:
            •      echo $this->Paginator->counter(array( 'format'             => __('Page %page%
            of %pages%, showing %current% records out of %count%              total, starting on
            record %start%, ending on %end%'')));

More Related Content

PDF
Using RequireJS with CakePHP
PDF
TurboGears2 Pluggable Applications
PDF
Getting to The Loop - London Wordpress Meetup July 28th
PDF
Extending the WordPress REST API - Josh Pollock
PPTX
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
PPT
Render API - Pavel Makhrinsky
PDF
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
PPTX
Planbox Backbone MVC
Using RequireJS with CakePHP
TurboGears2 Pluggable Applications
Getting to The Loop - London Wordpress Meetup July 28th
Extending the WordPress REST API - Josh Pollock
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
Render API - Pavel Makhrinsky
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Planbox Backbone MVC

What's hot (20)

PDF
Phinx talk
PDF
Introduction to AngularJS For WordPress Developers
PPT
jQuery and_drupal
PDF
Introduction to backbone presentation
PDF
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
PDF
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
PPTX
Cake PHP 3 Presentaion
PDF
Using Renderless Components in Vue.js during your software development.
PDF
Drupal 8 Services And Dependency Injection
PDF
You Don't Know Query - WordCamp Portland 2011
PDF
Drupal 8 Services
PDF
Django
PDF
Caldera Learn - LoopConf WP API + Angular FTW Workshop
PDF
Backbone
PDF
Rails 3: Dashing to the Finish
PDF
Keeping the frontend under control with Symfony and Webpack
PPTX
Working with WP_Query in WordPress
KEY
JavaScript in Drupal 7: What developers need to know
PDF
Using the new WordPress REST API
PDF
Drupal 8 Theme System: The Backend of Frontend
Phinx talk
Introduction to AngularJS For WordPress Developers
jQuery and_drupal
Introduction to backbone presentation
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Cake PHP 3 Presentaion
Using Renderless Components in Vue.js during your software development.
Drupal 8 Services And Dependency Injection
You Don't Know Query - WordCamp Portland 2011
Drupal 8 Services
Django
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Backbone
Rails 3: Dashing to the Finish
Keeping the frontend under control with Symfony and Webpack
Working with WP_Query in WordPress
JavaScript in Drupal 7: What developers need to know
Using the new WordPress REST API
Drupal 8 Theme System: The Backend of Frontend
Ad

Similar to How to migrate Cakephp 1.x to 2.x (20)

PPT
Synapseindia reviews sharing intro cakephp
PDF
CakePHP
PDF
Ch ch-changes cake php2
PDF
Pecl Picks
PDF
Say YES to Premature Optimizations
PPTX
PDF
Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached
PPTX
18.register login
ODT
PDF
Workshop quality assurance for php projects - phpbelfast
PDF
Workshop quality assurance for php projects - ZendCon 2013
PDF
Cakephp's Cache
 
KEY
Workshop quality assurance for php projects tek12
PDF
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
PPTX
PHP from soup to nuts Course Deck
PDF
Web services tutorial
PDF
HTTP Caching and PHP
PPT
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
PDF
Tips
PDF
Performance tuning with zend framework
Synapseindia reviews sharing intro cakephp
CakePHP
Ch ch-changes cake php2
Pecl Picks
Say YES to Premature Optimizations
Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached
18.register login
Workshop quality assurance for php projects - phpbelfast
Workshop quality assurance for php projects - ZendCon 2013
Cakephp's Cache
 
Workshop quality assurance for php projects tek12
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
PHP from soup to nuts Course Deck
Web services tutorial
HTTP Caching and PHP
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Tips
Performance tuning with zend framework
Ad

More from Andolasoft Inc (20)

PDF
Scalable Mobile App Development for Business Growth1 (1).pdf
PDF
Latest Facts and Trends in Fitness App Development
PDF
Challenges of React Native App Development_ Effective Mitigation Strategies.pdf
PDF
How To Use Server-Side Rendering with Nuxt.js
PDF
Essential Functionalities Your Real Estate Web App Must Have.pdf
PDF
A Complete Guide to Developing Healthcare App
PDF
Game-Changing Power of React Native for Businesses in 2024
PDF
A Complete Guide to Real Estate Website Development
PDF
How to Build Cross-Platform Mobile Apps Using Python
PDF
Impact of AI on Modern Mobile App Development
PDF
How to Optimize the SEO of Shopify Stores
PDF
14 Tips On How To Improve Android App Performance
PDF
The Ultimate Guide to Setting Up Your WooCommerce Store
PDF
Ranking The Best PHP Development Companies in the World
PDF
Top 8 WordPress Design and Development Trends of 2023
PDF
List of 10 Best WordPress Development Companies
PDF
WooCommerce vs Shopify: Which is Better For Your Online Store
PDF
Why Choose WooCommerce For Your eCommerce Store
PDF
Django Workflow and Architecture
PDF
Service Oriented Architecture in NodeJS
Scalable Mobile App Development for Business Growth1 (1).pdf
Latest Facts and Trends in Fitness App Development
Challenges of React Native App Development_ Effective Mitigation Strategies.pdf
How To Use Server-Side Rendering with Nuxt.js
Essential Functionalities Your Real Estate Web App Must Have.pdf
A Complete Guide to Developing Healthcare App
Game-Changing Power of React Native for Businesses in 2024
A Complete Guide to Real Estate Website Development
How to Build Cross-Platform Mobile Apps Using Python
Impact of AI on Modern Mobile App Development
How to Optimize the SEO of Shopify Stores
14 Tips On How To Improve Android App Performance
The Ultimate Guide to Setting Up Your WooCommerce Store
Ranking The Best PHP Development Companies in the World
Top 8 WordPress Design and Development Trends of 2023
List of 10 Best WordPress Development Companies
WooCommerce vs Shopify: Which is Better For Your Online Store
Why Choose WooCommerce For Your eCommerce Store
Django Workflow and Architecture
Service Oriented Architecture in NodeJS

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Modernizing your data center with Dell and AMD
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Big Data Technologies - Introduction.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Modernizing your data center with Dell and AMD
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Artificial Intelligence
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction

How to migrate Cakephp 1.x to 2.x

  • 1. How to migrate CakePHP 1.x to 2.x Keyword: CakePHP development, CakePHP 2.0, CakePHP migration We will focus on what we need to do to get CakePHP 1.3 application upgraded to CakePHP 2.2.3 CakePHP 2.2.3 is the latest stable version now. Installation • This is pretty straight forward, same as CakePHP 1.3 version. • Download and unzip the CalePHP 2.2.3 and follow the instructions. Configuration • core.php • Make sure to copy the security.salt and Security.cipher_seed from your old core.php • Take notice of how errors, sessions and caching have changed. • database.php • There is a minor change on this file, • CakePHP 1.3: 'driver' => 'mysqli' • CakePHP 2.2.3: 'datasource' => 'Database/Mysql' • routes.php • Don't overwrite this file with your older one. • You can place your custom routes before or after CakePlugin::routes() • bootstrap.php • Copy all functions, constants and other code from your old bootstrap into the new one. Folder Structure • The cake folder is now inside lib folder. There is nothing to do with this. • Files and folders are now CamelCased instead of lower_underscored • Example: The “users” folder inside the View becomes “Users” • The controller files are now UsersController.php instead of users_controller.php • project_user_controller.php becomes “ProjectUsersController.php” • The model files are now User.php instead of user.php • project_user.php model becomes “ProjectUser.php” • The components files are now FormatComponent.php instead of format.php • The helpers files are now DatetimeHelper.php instead of datetime.php Moved 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. Auth Component and Login • Auth now supports multiple authorization adapters that can all be used together • Cake 1.3.x was automatically checking if user has correctly entered username/password inside your login() method of users_controller but in cake 2.x we need to manually call $this->Auth->login() this returns boolean value based on successful login or failure.
  • 2. If you are using “email” as your login field name CakePHP 1.3: (inside AppController beforeFilter) $this->Auth->fields = array('username' => 'email', 'password' => 'password'); CakePHP 2.2.3: (inside AppController beforeFilter) $this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'email', 'password' => 'password'))); Auth Login CakePHP 2.2.3: (inside UsersController login function) if (!empty($this->request->data)) { if ($this->Auth->login()) { $this->redirect($this->Auth->redirect()); } else { //$this->Session->setFlash('Your Email or Password was incorrect.'); } } • CakePHP 2.x auth automatically hashes the password on login but not on save. We can add a beforeSave() method on the User model to hash the password. public function beforeSave($options = array()) { $this->data['User']['password'] = AuthComponent::password($this- >data['User']['password']); return true; } Request Data • CakePHP 2.0 has done some request related changes. • The Request Object does not have “form” element any longer. • You will need to replace $this->params['form'] with $this->request['data'] or $this- >request->data. • $this->data needs to be replaced with $this->request->data • So, now we can use $this->request->data on both form submit and AJAX post. • Now, we need to check !empty($this->request->data) instead of “!empty($this- >data)” while saving a form. Views Changes • Use $this->Html->link() instead of $html->link() • Use $this->Form-> instead of $form-> • Use $this->Session-> instead of $session-> • Use $this->Paginator-> intead of $paginator -> • For JavaScript inclusion use $this->Html->script(“”) instead of $javascript->link() Moved Email Elements • Email elements have been moved from views/elements to View/Emails.
  • 3. Helpers • The Ajax, Javascript, and XML helpers have been removed. You will need to replace these helper calls with appropriate alternatives • Helpers can no longer be called with “$helper->function()”. You need to update your helper calls to use $this->Helper->function() • If you are loading or importing your helpers inside another custom helper or component, • $myhelper = new DatetimeHelper() becomes $myhelper = new DatetimeHelper($this->_View) or $myhelper = new DatetimeHelper(new View(null)) Components • All component files should be extended by Component Class instead of Object DB and Code Caution • There is no Enum Support in cakephp2.x as sql server doesnt have similar datatype. • You can change enum to tinyint(2) • In cake 1.3 used tinyint(1) for is_active database fields. • While retrieveing from database it returns 0/1 • But, cakePHP2.x no longer returns as 0. This will return bool(true) /bool(false) • Boolean column values will be casted to php native boolean type automatically. __() Function and Pagination • The __() function no longer has the option to echo. It always returns • Change the Pagination code, • CakePHP 1.3: • echo $this->Paginator->counter(array( 'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%'', true))); • CakePHP 2.2.3: • echo $this->Paginator->counter(array( 'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%'')));