Create Custom Command in CodeIgniter 4

Share this Article
Reading Time: 7 minutes
459 Views

CodeIgniter 4 provides a rich set of Spark CLI Commands. By the help of CLI commands list we can manage everything. In previous versions Spark CLI commands are not available.

There are several newly added features in CodeIgniter 4 like migration, development server, seeder, spark, creating commands etc. So inside this article, we will Create Custom Command in CodeIgniter 4. To create a custom command we will use spark CLI tool.

We create custom command in CodeIgniter 4 to perform any certain specific task.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Understand make:command Spark Command

Open project into terminal and type the command

$ php spark

We should see the list of available spark commands. So in the list of commands we have a command called make:command as you can see

make:command is used to create custom command inside Spark CLI commands list.

Help Manual of make:command

To see the help manual of any spark command simply use this syntax.

$ php spark help <command-name>

For Example –

$ php spark help make:command

Create Custom Command

We can very easily create new commands to use in our own development. Each class must be in its own file, and must extend CodeIgniter\CLI\BaseCommand, and implement the run() method.

Commands must be stored within a directory named Commands i.e like /app/Commands

$ php spark make:command Test

Command Test will be created inside /app/Commands/Test.php

Open file and update this code into Test.php

<?php

namespace App\Commands;

use CodeIgniter\CLI\BaseCommand;

class Test extends BaseCommand
{
	/**
	 * The Command's Group
	 *
	 * @var string
	 */
	protected $group = 'My Command';

	/**
	 * The Command's Name
	 *
	 * @var string
	 */
	protected $name = 'test:call';

	/**
	 * The Command's Description
	 *
	 * @var string
	 */
	protected $description = 'This is a custom command created by online web tutor';

	/**
	 * The Command's Usage
	 *
	 * @var string
	 */
	protected $usage = 'test:call - It logs message inside application';

	/**
	 * The Command's Arguments
	 *
	 * @var array
	 */
	protected $arguments = [];

	/**
	 * The Command's Options
	 *
	 * @var array
	 */
	protected $options = [];

	/**
	 * Actually execute a command.
	 *
	 * @param array $params
	 */
	public function run(array $params)
	{
		//print_r($params);
		log_message("error", "This is sample error message logged from custom command");
	}
}
  • protected $group = ‘My Command’; It gives a group name to command
  • protected $name = ‘test:call’; This is command added into spark as a name to call it.
  • protected $description = ‘…’; This is description of command, what it will do.
  • protected $usage = ”; It will be used for help manual of this command
  • run() method is important, when we run this custom command to terminal then run() method will be called to process that.
  • log_message() It’s a codeigniter 4 helper function to create logs.

Back to project terminal and type

$ php spark

Successfully we have register our custom command into spark commands list.


Run Custom Command

Open project terminal. To run the custom command what we have created.

Let’s open help manual for test:call command.

$ php spark help test:call

Let’s run this command to test whether log has been created inside application or not.

$ php spark test:call

So here, we have implemented custom command is only for a simple task i.e log message inside application log. But apart from this concept we can use it various ways like – taking backup of database, making error logs of report etc.

$ php spark test:call param1 param2 param3

When we pass these values with command, then we can access all these by $params array variable type.

public function run(array $params) {
      print_r($params);
}

We can create custom command with group name, command name as well from terminal itself. Rest other things we can set by generated command file.

$ php spark make:command Test --group "Sample" --command "test:call"

It will add group name and command into Test.php file. It’s a short hand to generate the command file with available options.

For more detailed examples of Custom Spark Console command click here.

We hope this article helped you to learn about Create Custom Command in CodeIgniter 4 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment