Technology Blog

How to create custom artisan commands in Laravel

Leave a comment

Laravel ships in with some handy artisan commands which we use at different stages of our project. Some of them are more frequent like make:controller to create a controller and make:model to create a model and many more like this. But there may be requirement for some more commands which will be time saving for our project.

This tutorial is based on Laravel 5.2.

Objective:

In MVC architecture, there may be much more activities than just getting the request, make DB operations and pass the result to view. What I mean here is there may be some complex business logic which would not be ideal to be included in model. So what we will do is we will create a Services folder and keep all these complex business logics there for different modules we have.

Create console command:

php artisan make:console Service

We will use the above artisan command to create a new command Service. This will create a file Service.php inside app/Console/Commands directory with some default content which we will modify as per our requirement.

Set command signature and description:

protected $signature = 'make:service {file}';
protected $description = 'Create a new service class';

Signature is the command which user will type much similar to make:model and description tells the job of this command. The above signature has a mandatory argument called file which user has to pass. In this case, this will be the file name and the class name as well. If not provided, the following exception message will be shown

Not enough arguments (missing: "file").

Inform Artisan about this new command:

In app/Console there is a file called Kernel.php. We need to append our class to the $commands array so it would look something like

protected $commands = [
Commands\Service::class,
];

Now if you go to the terminal and hit php artisan, you will find the new command listed under make as below

make:service   Create a new service class

So we are done with adding our custom command and now we will add the job of this command. Since we will create a directory and a file inside that when someone uses this command, we will include the File facade to the Service.php file.

use File;

The handle function will be executed whenever someone runs this new command. First of all, we will check if the directory exists or not. If not, we will create that. I have put that directory path in a variable as we need to use that in multiple places.

$file_path = app_path('Services');

if (!File::exists($file_path)) {
File::makeDirectory($file_path);
}

The above will create a directory Services inside app directory only when it does not exist. Now we will retrieve the command argument as passed by the user and set that as file name and class name. We will also put the file content which includes the namespace and just the class name.

// Set the file name and content
$file_name = $this->argument('file') . '.php';
$file_content = "<?php" . "\n" . "namespace App\Services;" . "\n\n" . "class " . $this->argument('file') . "\n" .
"{}";
// Create the file with content in the above directory
File::put($file_path . '/' . $file_name, $file_content);

Then you need to set message which will be shown to the user as a result of the above operation. In our case, we show the file name and where it was created.

// Show message to the user
$this->info('Service file ' . $file_name . ' created successfully in ' . $file_path);

We are done with our new artisan command. Now go to your terminal and execute the following command

php artisan make:service BusinessService

You should see a new file BusinessService.php created in app/Services directory. That’s all. I hope this helps others 🙂

For more information on writing commands using artisan, please refer the official documentation here.

Author: Chittaranjan

I am a Zend PHP certified developer having expertise in Open Source web development. I have expertise in WordPress, Laravel, CodeIgniter, PHP, Javascript, jQuery, html, css with more than 12 years experience in professional open source development. I am a Sports Fanatic.

Leave a comment