Skip to content
Edit this page

Upgrading from 3.x to 4.x

CodeIgniter 4 is a rewrite of the framework and is not backwards compatible. It is more appropriate to think of converting your app, rather than upgrading it. Once you have done that, upgrading from one version of CodeIgniter 4 to the next will be straightforward.

The "lean, mean and simple" philosophy has been retained, but the implementation has a lot of differences, compared to CodeIgniter 3.

There is no 12-step checklist for upgrading. Instead, start with a copy of CodeIgniter 4 in a new project folder, however you wish to install and use it, and then convert and integrate your app components. We'll try to point out the most important considerations here.

To upgrade your project, there are two major tasks you have to work on. First of all, there are some general adjustments which are significant to every project and have to be handled. The second is the libraries, which make up CodeIgniter and contain some of the most important functions. These libraries operate separately from each other, so you have to look at them one by one.

Do read the user guide before embarking on a project conversion!

General Adjustments

Downloads

Namespaces

  • CI4 is built for PHP 8.2+, and everything in the framework is namespaced, except for the helper and lang files.

Application Structure

Important

index.php is no longer in the root of the project! It has been moved inside the public folder, for better security and separation of components.

This means that you should configure your web server to "point" to your project's public folder, and not to the project root.

If you use Shared Hosting, see Deployment to Shared Hosting Services.

  • The application folder is renamed as app and the framework still has system folders, with the same interpretation as before.
  • The framework now provides for a public folder, intended as the document root for your app.
  • The defined('BASEPATH') OR exit('No direct script access allowed'); line is not necessary because files outside the public folder are not accessible in the standard configuration. And CI4 no longer defines the constant BASEPATH, so remove the line in all files.
  • There is also a writable folder, to hold cache data, logs, and session data.
  • The app folder looks very similar to application for CI3, with some name changes, and some subfolders moved to the writable folder.
  • There is no longer a nested application/core folder, as we have a different mechanism for extending framework components (see below).

Routing

Model, View and Controller

  • CodeIgniter is based on the MVC concept. Thus, the changes to the Model, View and Controller are among the most important things you have to handle.
  • In CodeIgniter 4, models are now located in app/Models and you have to add the lines namespace App\Models; along with use CodeIgniter\Model; right after the opening php tag. The last step is to replace extends CI_Model with extends Model.
  • The views of CodeIgniter 4 have been moved to app/Views. Furthermore, you have to change the syntax of loading views from $this->load->view('directory_name/file_name') to echo view('directory_name/file_name');.
  • Controllers of CodeIgniter 4 have to be moved to app/Controllers. After that, add namespace App\Controllers; after the opening php tag. Lastly, replace extends CI_Controller with extends BaseController.
  • For more information, we recommend the following upgrade guides, which will give you some step-by-step instructions to convert the MVC classes in CodeIgniter4:

  • Upgrade Models

  • Upgrade Views
  • Upgrade Controllers

Core Class Changes

  • Input
    • CI3's Input corresponds to CI4's IncomingRequest.
    • For historical reasons, CI3 and CI4 used incorrect HTTP method names like "get", "post". Since v4.5.0, CI4 uses the correct HTTP method names like "GET", "POST".
  • Output

Class Loading

  • There is no longer a CodeIgniter "superobject", with framework component references magically injected as properties of your controller.
  • Classes are instantiated where needed, and framework components are managed by Services.
  • The Autoloader automatically handles PSR-4 style class locating, within the App (app folder) and CodeIgniter (i.e., system folder) top level namespaces, with Composer autoloading support.
  • You can configure the class loading to support whatever application structure you are most comfortable with, including the "HMVC" style.
  • CI4 provides Factories that can load a class and share the instance like $this->load in CI3.

Libraries

  • Your app classes can still go inside app/Libraries, but they don't have to.
  • Instead of CI3's $this->load->library('x'); you can now use $this->x = new \App\Libraries\X();, following namespaced conventions for your component. Alternatively, you can use Factories: $this->x = \CodeIgniter\Config\Factories::libraries('X');.

Helpers

  • Helpers are pretty much the same as before, though some have been simplified.
  • Since v4.3.0, you can autoload helpers via app/Config/Autoload.php, as in CI3.
  • Some helpers from CodeIgniter 3 no longer exist in Version 4. For all these helpers, you have to find a new way to implement your functions. These helpers are CAPTCHA Helper, Email Helper, Path Helper, and Smiley Helper.
  • Download Helper in CI3 was removed. You need to use the Response object where you are using force_download(). See Force File Download.
  • Language Helper in CI3 was removed. But lang() is always available in CI4. See lang().
  • Typography Helper in CI3 will be Typography Library in CI4.
  • Directory Helper and File Helper in CI3 will be Filesystem Helper in CI4.
  • String Helper functions in CI3 are included in Text Helper in CI4.
  • In CI4, redirect() is completely changed from CI3's.
    • redirect() Documentation CodeIgniter 3.x
    • redirect() Documentation CodeIgniter 4.x
    • In CI4, redirect() returns a RedirectResponse instance instead of redirecting and terminating script execution. You must return it from Controllers or Controller Filters.
    • Cookies and Headers you set before calling redirect() are not automatically carried over to a RedirectResponse. You need to call withCookies() or withHeaders() manually if you want to send them.
    • You need to change CI3's redirect('login/form') to return redirect()->to('login/form').

Hooks

  • Hooks have been replaced by Events.
  • Instead of CI3's $hook['post_controller_constructor'] you now use Events::on('post_controller_constructor', ['MyClass', 'MyFunction']);, with the namespace CodeIgniter\Events\Events;.
  • Events are always enabled, and are available globally.
  • The hook points pre_controller and post_controller have been removed. Use Controller Filters instead.
  • The hook points display_override and cache_override have been removed because the base methods have been removed.
  • The hook point post_system has moved to just before sending the final rendered page.

Error Handling

  • The behavior in CI4 has been slightly changed.
    • In CI3 the behavior is set in the index.php file:
      • errors with the error level set by error_reporting() are logged (but depending on the log_threshold setting, they may not be written to the log file).
      • errors with an error level of E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR stop framework processing, regardless of the error level set in error_reporting().
    • In CI4, the behavior is set in the app/Config/Boot/{environment}.php file:
      • errors with the error level set by error_reporting() are logged (but depending on the Config\Logger::$threshold setting, they may not be written to the log file).
      • all errors that are not ignored by error_reporting() will stop the framework processing.

Extending the Framework

  • You don't need a core folder to hold MY_... framework component extensions or replacements.
  • You don't need MY_X classes inside your libraries folder to extend or replace CI4 pieces.
  • Make any such classes where you like, and add appropriate service methods in app/Config/Services.php to load your components instead of the default ones.
  • See Creating Core System Classes for details.

Upgrading Libraries