大多数项目在业务发展过程中,都需要修复历史数据和定时任务来完成一些业务逻辑,这部分通常都需要通过脚本来完成,一般的框架爱也都提供这部分的功能,学习并使用是工作中的基本要求。
commands模式运行脚本定时任务基本流程:
app/Console/Commands/
目录下创建脚本任务文件app/Console/Kernel.php
$commands
数组中添加新建的脚本类app/Console/Kernel.php
schedule()
方法中添加脚本定时任务命令app/Console/Commands/QingShan/commandQingshan.php
<?php
namespace App\Console\Commands\QingShan;
use Illuminate\Console\Command;
class commandQingshan extends Command
{
// 自定义脚本命令签名
protected $signature = 'qingshan:commandQingshan';
// 自定义脚本命令描述
protected $description = '这里是脚本命令的描述qingshan';
// 创建一个新的命令实例
public function __construct()
{
parent::__construct();
}
//具体执行的业务内容
public function handle()
{
}
}
在app/Console/Kernel.php
$commands
数组中追加新建的脚本类
protected $commands = [
'BasicIT\LumenVendorPublish\VendorPublishCommand',
Commands\QingShan\commandQingshan::class
]
> php artisan list
在Available commands
下会有一列:
qingshan
qingshan:commandQingshan 这里是脚本命令的描述qingshan
> php artisan qingshan:commandQingshan
在app/Console/Kernel.php
schedule()
方法中添加脚本定时任务命令
// 设置commandQingshan脚本为每天15:00自动执行
protected function schedule(Schedule $schedule)
{
$schedule->command('qingshan:commandQingshan')->dailyAt('15:00');
}
参考资料:
原文:https://www.cnblogs.com/zqunor/p/11570789.html