How to Migrate a CodeIgniter 3 Web App to CodeIgniter 4

A Step-by-Step Tutorial for a CI3-toCI4 Upgrade

A Step-by-Step Tutorial for a CI3-toCI4 Upgrade

This guide details migrating from CodeIgniter3 to CodeIgniter4 using a `Book` example and introduces the CodeIgniter Wizard tool for automated code generation.

published on 9 Jul 2023 in CodeIgniter PHP

Upgrading from CI3 to-CI4

Moving your applications from CodeIgniter 3 (CI3) to CodeIgniter 4 (CI4) might appear daunting at first glance, but don't fret - it's a manageable process. Upgrading your CodeIgniter3 (CI3) project to CodeIgniter4 (CI4) requires a systematic process because of the extensive changes that were introduced in CI4. This article provides a to-the-point tutorial on how to migrate a typical CI3 application to CI4, using Book and BookModel as examples.

Step 1: Installation of CodeIgniter4 The first step is installing CodeIgniter4. You can do this by downloading it directly from the official website or using composer with the command:

composer create-project codeigniter4/appstarter your_project_name

Step 2: Migrate the Database Configuration You need to set up the database in app/Config/Database.php. For instance, let's migrate a typical CI3 database setup in application/config/database.php to CI4. CI3:

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => 'password',
	'database' => 'bookDatabase',
	'dbdriver' => 'mysqli',
);

CI4:

public $default = [
	'DSN'      => '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => 'password',
	'database' => 'bookDatabase',
	'DBDriver' => 'MySQLi',
];

Step 3: Migrate the Controller We will now migrate our BookController.php from CI3 to CI4. Here's a basic CI3 controller: CI3:

class Book extends CI_Controller {

	public function view($id)
	{
		$this->load->model('book_model');
		$data['book_item'] = $this->book_model->get_book($id);
		$this->load->view('book/view', $data);
	}
}

CI4:

namespace App\Controllers;

use App\Models\BookModel;
use CodeIgniter\Controller;

class Book extends Controller
{
	protected $bookModel;

	public function __construct()
	{
		$this->bookModel = new BookModel();
	}

	public function view($id)
	{
		$data['bookItem'] = $this->bookModel->getBook($id);
		return view('book/view', $data);
	}
}

Step 4: Migrate the Model Models in CI4 provide more powerful query builder support, better validation, and automatic database connection. Here's how to migrate your Book_model.php to BookModel.php: CI3:

class Book_model extends CI_Model {

	public function get_book($id)
	{
		$query = $this->db->get_where('book', array('id' => $id));
		return $query->row_array();
	}
}

CI4:

namespace App\Models;

use CodeIgniter\Model;

class BookModel extends Model
{
	protected $table      = 'book';
	protected $primaryKey = 'id';

	public function getBook($id)
	{
		return $this->where(['id' => $id])->first();
	}
}

Step 5: Migrate the View Views in CI4 function similarly to those in CI3. However, the syntax for loading views has changed slightly. Here's a CI3 view being migrated to CI4:


CI3 code:


<?php echo $book_item['title'] ?>

CI4 version:


<?= $bookItem['title'] ?>

Step 6: Update the Routes In CI3, routes were defined in application/config/routes.php. In CI4, routes are placed in app/Config/Routes.php. CI3:

$route['book/(:any)'] = 'book/view/$1';

CI4:

$routes->get('book/(:segment)', 'Book::view/$1');

If you have a Mac with macOS 10.13 or later, CodeIgniter Wizard for Mac can automatically create CI4 models, views and controllers, even separate entity classes reverse-enginrering an existing database.

For those who do not already know, CodeIgniter Wizard is a powerful tool for developers who prefer a graphical user interface (GUI) over writing code manually. It can automatically generate models, views, and controllers for CodeIgniter 4 applications, speeding up development time significantly. Not only does it reverse engineer an existing database to create entity classes, but it also provides CRUD scaffolding and form validation rules.

Migrating from CI3 to CI4

Even though it offers a convenient way to create applications, the auto-generated code may not always meet all your requirements. Thus, knowing how to manually migrate from CI3 to CI4 is still a valuable skill. Additionally, some developers prefer to code by hand to have more control and understanding of their application's structure and behaviour.

Nevertheless, let's see how to use CodeIgniter Wizard to migrate our example Book entity from CI3 to CI4:

  1. Install CodeIgniter Wizard: Download the latest version from the official website and install it on your Mac.

  2. Connect to your database: Open CodeIgniter Wizard and enter your database credentials to connect. You should see a list of your tables appear.

  3. Generate Entity Class: Select your Book table and click on "Generate Entity Class". CodeIgniter Wizard will automatically create a PHP class with properties that correspond to the fields of your Book table.

  4. Generate Model: Next, click on "Generate Model" to create a BookModel class. The generated class will extend CodeIgniter\Model and include methods for the CRUD operations.

  5. Generate Controller: Click on "Generate Controller" to create a Book controller with methods for viewing, creating, updating, and deleting books.

  6. Generate Views: Finally, click on "Generate Views" to create views for the different operations. You'll get separate view files for the list, create, edit, and view details.

CodeIgniter Wizard can be a significant time saver for migrating your CI3 application to CI4. If the generated code may not suit all your needs, you can always modify it manually to achieve your desired functionality.

In conclusion, while the differences between CI3 and CI4 can seem vast, once you understand the basics of migration, the process can be straightforward. CI4's new features offer a more robust and flexible framework that's better suited to modern web development. This guide provides a concrete starting point for migrating your applications from CI3 to CI4. However, each application is unique, and some may require additional steps. For more complex applications, the official CodeIgniter4 user guide is an excellent resource.

Share this on

Comments

What do you think?

  • Prifaj
    Prifaj
    I'm curious about the CodeIgniter Wizard tool you mentioned. It seems like a great time-saver for developers, especially for those working on larger projects. However, I wonder how flexible it is in terms of customization? Can it handle more complex database structures and relationships? Also, is there a similar tool available for developers who don't use Mac?
    • Ozar
      Ozar
      CodeIgniter Wizard - as mentioned in the blog post - is designed to speed up the development process by automatically generating models, views, and controllers for CodeIgniter 4 applications. It can reverse engineer an existing database to create entity classes, provide CRUD scaffolding, and form validation rules. This suggests a certain level of flexibility and capability to handle complex database structures and relationships. However, the extent of its customization capabilities and its ability to handle more complex scenarios would depend on the specific features of CodeIgniter Wizard such that up to the current versions: 1- All views it creates are Bootstrap-5 based. 2- It creates separate files for form fields which could be swapped between different layout templates 3- List views are currently and exclusively based on datatables.net with supporting both server-side (for lazy-loading) and front-end only loading and pagination.