Codeigniter 4 Top 10 features


Hi fellow developers, Codeigniter 4 has arrived recently with some greatest & much-needed improvement. Codeigniter 4 framework is completely rewritten & restructured & it’s not backward compatible. The directory structure of the framework has been changed significantly. The naming convention of files & directory are also changed, now it follows the camel-case convention for naming & All the directory names start with a capital letter.

So let’s deep dive into the Codeigniter 4 framework & discuss what’s it got for the modern-day developers. The following are my observations of the new framework & it’s top improvements/features I like.

1.Command Line Tool: Spark

Codeigniter 4 has introduced a new command-line tool called Spark. If you ever used Laravel than you might be familiar with this approach already. A command tool helps you to do some basic tasks with ease. For example, creating a migration file in the case of Codeigniter 4 which was missing in the CI3 & we had to use some third party code to create a command-line tool that could generate the migration files.

The spark is still lacking many features & not as powerful as artisan tool(Laravel’s command-line tool) but it is a good start. CI4 lets you create your own custom commands which are a very useful feature. So you can create your own command & use them. To know which features or commands are available to use you can get that by firing this command & it will list all the available commands for you.

> php spark list

The output will be like this,

Spark list command

Spark list command

2.Local Development Server

Another good improvement is the now CI4 utilizes the PHP’s inbuilt server. Using this feature you can now create a local development server from your project directory without needing to move the project to htdocs folder.

This is the part of the Spark command-line tool. You can fir this command & a local development server will start the at the predefined port number & will start logging the incoming request in the terminal.

> php spark serve
Starting Local Development Server

Starting Local Development Server

3. Introducing Namespaces.

Finally, Codeigniter developers have introduced namespacing in the framework. Is was really needed for the modern-day architecture & framework. Now all the controllers, Models & Libraries are namespaced.

4. Improved Routing Mechanism.

Routing wast the thing which I mostly hated in the Codeigniter 3. For me CI3 routing sucks & it was not that intuitive. Finally, they have fixed this problem & I am very happy about it. They have introduced some great features like HTTP verb methods. You can directly call the get() or post() instead of using them as an array key in CI3.

$routes->get('products', 'Product::feature');
$routes->post('products', 'Product::feature');
$routes->put('products/(:num)', 'Product::feature');
$routes->delete('products/(:num)', 'Product::feature');

Route Groups: This is another great feature that I wish we had in the CI3 as grouping routes come very handy when creat APIs endpoints. Now you can group the routes like this,

$routes->group('api', function($routes)
    $routes->group('products', function($routes)
        $routes->get('list', 'Products::list');
       $routes->post('add', 'Products::add');


Closure: You can add closure in the routes which are another good feature CI4 have in the routing.

$routes->add('hello', function()
    return "This is a closure";

One thing which I did not like is that why they put the Routes.php in the config folder. It should have its own separate folder for it.

There are still many great features it has which I cannot cover here all. To know more about the routing head towards the official documentation here.

5. Migrations & Database Seeding.

The migrations are a great way to manage your database structure.CI3 have the migration feature for managing database but it lacked the tooling for creating & managing the migrations. Now CI4 has a new command-line tool using which you can create, refresh & rollback your migrations.

You can create new migration file using the command,

> php spark migrate:create test  

CI4 still uses the dbforge library for managing the migrations So you don’t have to worry much about using the migrations in the CI4 if you have used migration in CI3.

Database Seeding is an addition to the CI4 framework. The seeding mechanism helps you populate your tables with a random or specified data set. I don’t know about you but this feature is really useful for me as it comes handy when I want to test my application with a large dataset. I don’t have to insert data manually in the tables.

In CI3 I had to use someone’s code from the Internet to create & manage the database seeder files which annoyed me. Thanks to the CI4 team that they have realized that it is a very important feature for any modern-day framework.

You can create a seeder file in the Database/Seeds directory. They should have a command-line option to create the seed files. This is the example seed file from the official documentation,

class SimpleSeeder extends \CodeIgniter\Database\Seeder
        public function run()
                $data = [
                        'username' => 'darth',
                        'email'    => ''

                // Simple Queries
                $this->db->query("INSERT INTO users (username, email) VALUES(:username:, :email:)",

                // Using Query Builder

6. Improved Database Modeling.

The CI4 team has done major improvements in the way Ci4 now handles the database modeling. In CI3 you just had a CI_Model which you need to extends to fire queries on database tables. CI_Model only provides you with the bare bone & you have to build your own custom methods as you go. CI4 now offers features like soft deletes, record timestamps, model events, etc features which CI3 had lacked.

Apart from the Query Builder class CI4 provides optional Entity classes. By using entity class for the model you can structure how each of the records will look like. Entity class helps you structure your table records which are returned by the SQL query. You can manipulate any column value the way you want. I might write an article in detail about how you can use & benefit from Entity classes. For time being you want to know more about it then read it here.

7. Inbuild Support For API Creation.

In this modern world application development, you have to deal with API development. If you have not built an API using the CI3 than consider yourself lucky because API development in CI3 is a big pain in the ass. You have to use the third party library to build any API in CI3 & the thing is that all the third-party libraries suck when it comes to API development. They do provide some base for API development but overall they are not much useful.

To address this problem Ci4 has introduced Resource Routes. By using this you can quickly create API endpoint for a single controller like this,


So it will map all the routes to their respective methods. Here the way we create methods will remain the same. Here are some equivalent routes will look like for the above resource route,

// Equivalent to the following:
$routes->get('product/new',                'product::new');
$routes->post('product/create',            'product::create');
$routes->post('product',                   'product::create');   // alias
$routes->get('product',                    'product::index');
$routes->get('product/show/(:segment)',    'product::show/$1');
$routes->get('product/(:segment)',         'product::show/$1');  // alias
$routes->get('product/edit/(:segment)',    'product::edit/$1');
$routes->post('product/update/(:segment)', 'product::update/$1');
$routes->get('product/remove/(:segment)',  'product::remove/$1');
$routes->post('product/delete/(:segment)', 'product::update/$1');

Read more about the resource route in the documentation here.

8. New Inbuilt Debug Tool.

Debugging in PHP is a kind of nightmare. There are some debuggers that are available for PHP but I find them difficult to setup. I have tried to set up one but failed miserably. I use Laravel for some of my personal projects. I have found that they have made debugging a painless process by developing a package called Telescope(official package). Telescope provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps and whatnot. Believe me, my friends I have not seen a much useful & powerful package than Telescope in the Laravel ecosystem.

CI4 has introduced its own GUI debugging tool like Telescope.CI4 has adopted the Kint debugging package to create this debugging toolbar (Kint is an independent package). This toolbar is enabled by default on the local environment. To see this in action you have to create a rouet & page. Open that route in the browser & you will see tiny CI logo either top left corner or bottom right depending on the setting & you can change that also. When you click on the logo it will look like this,

CI4 Debugging Tool

CI4 Debugging Tool

Here you can see various tabs for memory, routes, files, vars for var dumps, etc. These all provide insight into your application. In the last tab, you can see all the variables & their values which we need most of the time while debugging our applications.

Var Dumps Tab

Var Dumps Tab

9.Inclusion Of .env File.

In software development, we have put our application on different environments for different stages like local server, development server, testing server & a production server. For all these different environments we have to use a different configuration for base URL, database, mail SMTP settings & many other things.

To manage all those environments in CI3 we have to create different directories for where I have to put different config files there then you have to go to index.php file to add some lines so that it knows from which directory it has to load the config files. I did not like this way & it always seems a hacky thing to me.

The best way to do such thing according to the (It is a site which tells you about the best practices in software development. followed & recommended by many expert developers) is to use a config file which usually looks like this .env. We have stored all the settings as key/value pairs. For every environment, you will have the same keys but different values. Those key/value pairs are called environment variables & Codeigniter 4 provides a global function env() to access any of those values. Codeigniter 4 automatically detects a .env file & loads that into the memory. Now you don’t need to create a different folder for different environment config files.

10. Testing

Testing in software development is like going to the gym. Everybody knows its good for you. Everyone thinks that they should do it & many people recommend it but still, most of us don’t do it.

CI4 has put a great emphasis on the testing in this version. CI3 has one unit testing library which I guess nobody uses & only God knows how to use them properly for testing. To be honest I never used that thing for unit testing. It lacks many things & has no proper documentation.

CI4 now offers you database testing, Controller testing & HTTP request testing which is more than sufficient for small to medial scale application.


For me, Its just not the new version but rebirth of the Codeigniter framework. From the past couple of years, people used to say that Codeigniter ha no place for modern-day applications & its a dying framework. I have used CI professionally but as people said it lacked the features to build a modern-day application which I had to admit was true. So CI4 got lots of features to offer & it looks promising to me I can’t wait to build my next application using this framework.

What do you think about this new Codeigniter version? Please let me know in the comments.

Ropali Munshi
Ropali Munshi
Ropali Munshi is fullstack PHP Developer. He is passionate developer who loves to learn and expirement with new programming languages , libraries and frameworks. Nowdays he is more into the JavaScript realm.

Leave a Reply

Your email address will not be published. Required fields are marked *