Upgrading PHP CodeIgniter 2.x version project to 3.x

Upgrading CodeIgniter 2.x to 3.x

Following are the step to upgrade your legacy CodeIgniter 2.x application into CodeIgniter 3.x application. CodeIgniter 3.x supports new versions of PHP and has more security and faster execution.

1- Update your CodeIgniter files

Go to system folder replace all files and directories in your system/ directory and replace your index.php file.
If any modifications were made to your index.php they will need to be made fresh in this new one.
if you have any custom developed files in system folder please make a copy of them first.

2- Move your Log class overrides or extensions

The Log Class is considered as a core class and is now located in the system/core/ directory. Therefore, in order for your Log class overrides or extensions to work, you need to move them to application/core/:

application/libraries/Log.php -> application/core/Log.php
application/libraries/Input.php -> application/core/Input.php

3- Update your classes file names

In Codeigniter 3.x all class filenames (controllers, model, drivers, libraries) must start with a capital letter. For examples:

Library

application/libraries/mylibrary.php -> application/libraries/Mylibrary.php

Controllers

application/controllers/welcome.php -> application/controllers/Welcome.php

Models

application/models/welcome_model.php -> application/models/Welcome_model.php

4- Replace config/mimes.php

mimes.php config file has been updated in 3.x to contain more user mime-types. Please replace mimes.php config file with new 3.x codeigniter mimes.php config file.

5- Update your Session library usage

The Session Library has been completely re-written in CodeIgniter 3 and now comes with a new features.

  • The table session table structure has changed a bit:
  • session_id field is renamed to id
  • user_agent field is dropped
  • user_data field is renamed to data and under MySQL is now of type BLOB
  • last_activity field is renamed to timestamp
  • Before Codeigniter 3.x unset_userdata function used to accept an associative array of ‘key’ => ‘value’ pairs for unsetting multiple keys.
  • Now Codeigniter 3.x only key as the elements of array no need to pass value.
    $this->session->unset_userdata(array('name' => 'abc', 'email' => '[email protected]')); -> $this->session->unset_userdata(array('name', 'email'));

6- Replace your error templates

In CodeIgniter 3.x the error templates are now considered as views and have been moved to the application/views/errors/html directory. Furthermore in Codeigniter 3.x, there is support for CLI error templates in plain-text format that unlike HTML. You can move your old files from application/errors to application/views/errors/html but you have to copy the new application/views/errors/cli directory from the CodeIgniter 3.x and past into application/views/errors/cli directory.

7- Update Config/database.php file

In Codeigniter 3.x renaming of Active Record to Query Builder inside your config/database.php.
you will need to rename the $active_record variable to $query_builder with true value.

// $active_record = TRUE;
$query_builder = TRUE;

8- Update your config/routes.php any wildcard

CodeIgniter has always provided the :any wildcard in routing. In Codeigniter 2.x the :any wildcard is represent with .+.
This is considered a bug as it also matches the / (forward slash) character which is the URI segment delimiter.
In CodeIgniter 3 the :any wildcard will now represent [^/]+, so that it will not match a forward slash.

9- Update usage of Database Forge’s drop_table() method

In Codeigniter 3.x the IF EXISTS condition is no longer added by default and has an optional second parameter [TRUE]
and is set to FALSE by default.
$this->dbforge->drop_table(‘table_name’); -> $this->dbforge->drop_table(‘table_name’, TRUE);

10- Many functions now return NULL instead of FALSE on missing items

Many methods and functions now return NULL instead of FALSE when the required items don’t exist.
uri->segment(), session->flashdata();

11- Remove previously deprecated functions

The SHA1 library

In Codeigniter 3.x previously deprecated SHA1 library has been removed now your code to use PHP’s native sha1() function to generate a SHA1 hash.

Security helper do_hash()

In 3.x, Codeigniter now uses PHP native hash() function instead of do_hash() function.

String helper trim_slashes()

In Codeigniter 3.x now uses trim() function instead of trim_slashes().

Form helper form_prep()

In Codeigniter 3.x now uses html_escape() function instead of form_perp() function.

Date helper standard_date()

Date Helper function standard_date() is being deprecated in Codeigniter 3.x due to the availability of native PHP constants. Which when combined with date() function provide the same functionality.

12- Upgrade your CodeIgniter 2 application now!

You can email to discuss with CodeIgniter expert to update your 2.x application to a newer 3.x and 4.x versions.

Related: 8 Steps to CodeIgniter performance optimization