Create Custom Spark Console Command in CodeIgniter 4

Reading Time: 11 minutes
6,090 Views

In CodeIgniter 4, we have several spark commands available which is designed to complete some specific task. There are several sections like Database, Generators, Housekeeping etc.

Now, here we will create custom spark console command in CodeIgniter 4. Inside this article we will cover two different sections.

  • Command which Takes User Inputs and Display Information
  • Command Takes input from User and save to database by Model object
  • Command which generates 50 fake rows data and save into database.
  • Command To Change Spark Default Port, click here

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

Command which Takes User Inputs and Display Information

Open project into terminal and run this command.

$ php spark make:command Userinfo

It will creates a file Userinfo.php at /app/Commands folder.

Open Userinfo.php and write this piece of code.

Userinfo.php

Back to terminal and type this command –

$ php spark

Inside PHP spark command list, we should see own custom created command.

Command Testing

Write this command to terminal and hit enter.

$ php spark user:info

It will ask few questions and display user information.


Inputs from User and save to database by Model

Let’s create a database and a table. Connect database with .env at your own.

# Database
CREATE DATABASE codeigniter4_app;

# Table
CREATE TABLE `users` (
 `id` int(5) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(100) NOT NULL,
 `email` varchar(100) NOT NULL,
 `mobile` varchar(25) DEFAULT NULL,
 `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Next, we need to create a Model. We will spark command to create it.

$ php spark make:model User --suffix

make:model will create a model file with name UserModel.php at /app/Models folder.

Open UserModel.php and write this code.

UserModel.php
$ php spark make:command Usercreate

make:command will create a file Usercreate.php at /app/Commands

Open Usercreate.php and write this code into it.

Usercreate.php

Command Testing

Back to terminal & run this command.

$ php spark user:create

Generates 50 fake rows data and save into database

We will consider the same database, table & model what we have created for above command.

$ php spark make:command Fakeusers

It will creates a file Fakeusers.php at /app/Commands.

Open Fakeusers.php and write this code into it.

Fakeusers.php

Command Testing

Back to terminal & run this command.

$ php spark fake:users

It will create 50 fake users and save to database.

We hope this article helped you to learn about Create Custom Spark Console Command in CodeIgniter 4 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.