How to Create Custom Artisan Console Commands in Laravel 5.1 Framework


For beginners in laravel, you may not know much about creating custom Artisan console commands in laravel. The following article will share with you an example of creating custom Artisan console commands in laravel framework. 1. Getting started

Laravel provides powerful console commands to handle non-browser business logic through Artisan. To view all the Artisan commands in Laravel, you can run them at the project root directory:

php artisan list

The corresponding output is as follows (partial screenshot):

Some of them are familiar to us, such as creating migration make:migration and executing migration, and creating model make:model and controller make:controller.

If you want to see the specific usage of a command, for example, we want to see the specific usage of the create Artisan command make:console, you can use the following command:

php artisan help make:console

The corresponding output is as follows:

2. Create a command

Besides providing abundant console commands, Artisan also allows us to create our own console commands through the make:console command. We have used the help command to check the usage of make:console above. Let's go along this road and find out: create commands and run them to get all kinds of results we want.

First of all, we create the simplest command, print Hello LaravelAcademy, and use the Artisan command as follows:

php artisan make:console HelloLaravelAcademy --command=laravel:academy

Where HelloLaravelAcademy is the command name, laravel:academy is the command executed by the console, similar to make:console.

After the execution is completed, a HelloLaravelAcademy.php file will be generated in the app/Console/Commands directory:

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us