Currently, with the PHP language, the Laravel framework is one of the most popular frameworks today. There are many projects that use Laravel to deploy the project because of its great features. And the Artisan command is one of those great features. By default, Laravel provides us with some basic commands for our application development. So how can we create the command we want, learn through our article offline
Artisan is built on the Symfony Console component
Some basic commands that the application has provided us:
- Get all available Artisan commands:
1 2 | php artisan list |
- When you want help with details about a command you use, arguments and options, we can use the following command:
1 2 | php artisan help command_name |
For example: We want to know the details of the command migrate:
1 2 | php artisan help migrate |
The terminal interface will show us the details of the command to migrate
The commands will usually be saved in the app/Console/Commands
folder. However, you can also choose where you save the command as long as your command is loaded by Composer, you can also install the composer load in the composer.json
file.
To create a new command we use the command Artisan make:command
that laravel has provided us. The new command will create a new class, which is saved in the app/Console/Commands
folder. If the application does not have this directory, then after creating a new command using the Artisan make:command
, the app/Console/Commands
will be created.
After creating we go to kernel.php
file to declare the command
We create a new command with the following command:
1 2 | php artisan make:command customCommand --command hello:name |
For details about the make:command
and its options we use the command we introduced above:
1 2 | php artisan help make:command |
As in its options, I have added the --command hello:name
option to assign the command name. After executing the command to create the command above we open the app/Console/Commands/CustomCommand.php
to edit the signature
and description
of the command we created.
Here I will correct as follows:
1 2 3 4 5 6 7 8 9 10 11 12 | <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'hello:name {name?}'</span> <span class="token punctuation">;</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token keyword">protected</span> <span class="token variable">$description</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'This is custom Artisan command'</span> <span class="token punctuation">;</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">handle</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">argument</span> <span class="token punctuation">(</span> <span class="token single-quoted-string string">'name'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token variable">$this</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">info</span> <span class="token punctuation">(</span> <span class="token double-quoted-string string">"Hello <span class="token interpolation"><span class="token variable">$name</span></span> , This is custom Artisan command"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> |
fuction handle()
will be called when we execute the command. Here I will get the name
that we enter when executing the command and display Hello name, This is custom Artisan command
.
Then we go to the kernel.php
file and add our command:
1 2 3 4 | <span class="token keyword">protected</span> <span class="token variable">$commands</span> <span class="token operator">=</span> <span class="token punctuation">[</span> Commands <span class="token package">CustomCommand</span> <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token keyword">class</span> <span class="token punctuation">,</span> <span class="token punctuation">]</span> <span class="token punctuation">;</span> |
After performing the above steps, we check the command with the php artisan list
And to see the details of the command we execute: php artisan help hello: name ”
Finally, let’s run the command we created:
1 2 | php artisan hello:name Huynh |
The results will show:
1 2 | Hello Huynh, This is custom Artisan command |