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
Command | Description |
---|---|
php spark serve | Start 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 migrate | Run all new migrations |
php spark db:seed <seeder> | Run a specific database seeder |
php spark cache:clear | Clear 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.