Home > Hosting > System

How to Create Custom Artisan Console Commands in Laravel 5.1 Framework

2024-01-28 06:25:46

<p>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</p><p>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:</p><p>php artisan list</p><p>The corresponding output is as follows (partial screenshot):</p><p><img src="https://freeonlinedomain.com/uploads/allimg/Server/20240128/2zxdbovgthb26945.jpg"/></p><p>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.</p><p>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:</p><p>php artisan help make:console</p><p>The corresponding output is as follows:</p><p><img src="https://freeonlinedomain.com/uploads/allimg/Server/20240128/dqsd123r1hl26946.jpg"/></p><p>2. Create a command</p><p>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&#39;s go along this road and find out: create commands and run them to get all kinds of results we want.</p><p>First of all, we create the simplest command, print Hello LaravelAcademy, and use the Artisan command as follows:</p><p>php artisan make:console HelloLaravelAcademy --command=laravel:academy</p><p>Where HelloLaravelAcademy is the command name, laravel:academy is the command executed by the console, similar to make:console.</p><p>After the execution is completed, a HelloLaravelAcademy.php file will be generated in the app/Console/Commands directory:</p>


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