1、创建命令
php artisan make:command Houxin
此时会在App/Console下面生成Commands文件夹,然后里面有你新建的Houxin的命令文件
2、打开命令文件
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Houxin extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:houxin';
/**
* 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()
{
//
add_log('hello world');
}
}
此时的$signature就是你的命令名称,最初的名称为command:name,现在这里我改成command:houxin.
3、查看命令
php artisan list
此时会显示的命令
command
command:houxin Command description
4、执行命令并处理结果
执行
php artisan command:houxin
的时候,就会自动执行你的类里面的handle里面的代码。
这里的是一个写入日志的操作。就会看到我的写入日志的代码