Search

Laravel Artisan Generator Command: The make:command Command

December 7, 2016 —John Koster

The make:command command can be used to generate a new command class. The class will be created in the app/Console/Commands directory. The command requires a name argument to be supplied. This name will be the name of the newly generated command class and file. An optional --command option can be supplied, which can be used to assign the terminal command name (as an example make:command is the terminal command name of the ConsoleMakeCommand class).

In Laravel 5.3, the make:console command was renamed to make:command. This section is relevant for Laravel 5 versions 5.2 and below with the command make:console.

The following example demonstrates how to call the make:command command with all options for a hypothetical command that would quickly uninstall the application:

1php artisan make:command QuickUninstallCommand --command=dev:uninstall

After the above command has ran, the following class would be created in the app/Console/Commands/QuickUninstallCommand file. Notice that the dev:uninstall value supplied for the --command option appears on line 14:

1<?php
2 
3namespace App\Console\Commands;
4 
5use Illuminate\Console\Command;
6 
7class QuickUninstallCommand extends Command
8{
9 /**
10 * The name and signature of the console command.
11 *
12 * @var string
13 */
14 protected $signature = 'dev:uninstall';
15 
16 /**
17 * The console command description.
18 *
19 * @var string
20 */
21 protected $description = 'Command description';
22 
23 /**
24 * Create a new command instance.
25 *
26 * @return void
27 */
28 public function __construct()
29 {
30 parent::__construct();
31 }
32 
33 /**
34 * Execute the console command.
35 *
36 * @return mixed
37 */
38 public function handle()
39 {
40 //
41 }
42}

The make:command command is also capable of generated namespaced classes. Accomplish this by separating namespace sections using the \ when supplying the command name. A nested directory will be created and the correct namespace will be set on the generated class.

1# Generate a new console command with a nested namespace.
2php artisan make:command Namespaced\ExampleCommand

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.