CodeIgniter 4 Spark CLI Tutorial [New Feature] ๐Ÿš€

Reading Time: 5 minutes
21 Views

CodeIgniter 4 introduced a amazing and interactive Command Line Interface (CLI) tool named Spark to streamline development workflows. Similar to Laravelโ€™s Artisan, Spark CLI simplifies tasks like creating controllers, models, migrations, and managing the application without manually writing boilerplate code and much more.

In this tutorial, we’ll explore:

  • What is Spark CLI?
  • How to use it
  • Available commands
  • Creating custom commands
  • Practical examples

Let’s get started.

๐Ÿ“Œ What is CodeIgniter 4 Spark CLI?

CodeIgniter 4 comes with a built-in tool called Spark CLI that lets developers work with their applications via the terminal. It offers a command-based interface for swiftly and reliably completing standard development activities.

๐Ÿ‘‰ Spark CLI is located at the root of your CodeIgniter 4 project.

php spark

Read More: CodeIgniter 4 Authentication

๐Ÿ“Œ How to Access Spark CLI

Ensure you have PHP installed and your working directory is at the projectโ€™s root.

Then, in your terminal:

php spark

Youโ€™ll see a list of available commands.

๐Ÿ“Œ Commonly Used Spark Commands

CommandDescription
php spark serveStart the local development server
php spark make:controller <name>Create a new controller
php spark make:model <name>Generate a new model
php spark make:migration <name>Create a database migration file
php spark migrateRun all new migrations
php spark db:seed <seeder>Run a specific database seeder
php spark cache:clearClear application cache

๐Ÿ“Œ Examples of Using Spark CLI

๐Ÿ”ธ Start Local Server

php spark serve

By default, runs on http://localhost:8080/

๐Ÿ”ธ Create a Controller

php spark make:controller Blog

Generates app/Controllers/Blog.php

Options:

php spark make:controller Blog --restful

Creates a RESTful controller structure.

๐Ÿ”ธ Create a Model

php spark make:model PostModel

Generates app/Models/PostModel.php

Options:

php spark make:model PostModel --entity

Creates a model using an Entity class.

๐Ÿ”ธ Create a Migration

php spark make:migration CreatePostsTable

Generates a migration file in app/Database/Migrations

Then run:

php spark migrate

๐Ÿ”ธ Create a Seeder

php spark make:seeder PostSeeder

Then execute:

php spark db:seed PostSeeder

๐Ÿ“Œ View All Available Commands

To see all commands, simply run:

php spark

Youโ€™ll get a complete list of available commands with descriptions.

๐Ÿ“Œ Creating Custom Commands

CodeIgniter 4 allows you to extend the CLI by creating custom commands.

Steps:

>> Create a new file in app/Commands by using this command,

php spark make:command GreetUser

>> It generates app/Commands/GreetUser.php

>> Inside the class, customize:

protected $group       = 'custom';
protected $name        = 'greet:user';
protected $description = 'Greets the user by name';

public function run(array $params)
{
    $name = $params[0] ?? 'Guest';
    echo "Hello, {$name}!";
}

>> Run your custom command:

php spark greet:user Sanjay

>> Output

Hello, Sanjay!

๐Ÿ“Œ Useful Spark Command Groups

make: For generating boilerplate files (controllers, models, migrations)

migrate: For running, rolling back, or refreshing migrations

db: For seeding database data

cache: Managing application cache

๐Ÿ“Œ Conclusion

CodeIgniter 4 Spark CLI is an essential tool for modern PHP developers working with the framework. It not only accelerates routine development tasks but also ensures consistency, reduces human errors, and promotes clean, maintainable code structures.

By mastering Spark CLI commands, you can:

  • Quickly generate controllers, models, migrations, and seeders without manual boilerplate.
  • Manage database migrations effortlessly.
  • Start a built-in development server in seconds.
  • Clear caches and optimize your application with simple terminal commands.
  • Extend your development workflow by creating custom CLI commands tailored to your projectโ€™s needs.

The real power of Spark CLI lies in how it standardizes repetitive processes, freeing you to focus on building features and solving problems instead of handling setup and configuration manually.