Home >PHP Framework >Laravel >Let's talk about how laravel uses commands to execute scripts

Let's talk about how laravel uses commands to execute scripts

藏色散人
藏色散人forward
2021-11-23 15:43:442771browse

The following tutorial column of Laravel will introduce how to use laravel commands to execute scripts. I hope it will be helpful to everyone!

laravel Use the command to execute the script

1. Generate the console file

 php artisan make:console TestConsole --command=laravel:test

2. Edit the business logic in the handle method

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class HelloLaravelAcademy extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = &#39;laravel:test {name?}&#39;;
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = &#39;Command description&#39;;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        echo $this->argument("name").PHP_EOL;
        echo 123;
    }
}

$signature indicates whether to receive parameters, ? indicates that it is not required. arguement receives parameters

execute

 php artisan laravel:test
 
 php artisan laravel:test xiaoming

[Related recommendations: The latest five Laravel video tutorials

The above is the detailed content of Let's talk about how laravel uses commands to execute scripts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete