How To Create Custom Command in CodeIgniter 4

Reading Time: 7 minutes
5,594 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.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# 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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.