- 生成console文件
php artisan make:console TestConsole --command=laravel:test
复制代码
- handle方法中编辑业务逻辑
<?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 = 'laravel:test {name?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* 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为是否接收参数,?表示非必须。 arguement接收参数
- 执行
php artisan laravel:test
php artisan laravel:test xiaoming
复制代码