慕容的工作随笔

慕容的工作随笔

记录学习和工作

【原创】Laravel 定时计划任务

定时计划任务文件

在 Console/Commands 下创建计划任务文件,例如:DeleteMatchCrontab.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class DeleteMatchCrontab extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'crontab:delete-match';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = &#039;删除匹配记录&#039;;

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    // 处理业务内容
}

}

注册计划任务

Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Artisan;

class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
Artisan::call("crontab:delete-match");
})->dailyAt('1:30');
}

/**
 * Register the commands for the application.
 *
 * @return void
 */
protected function commands()
{
    $this-&gt;load(__DIR__.&#039;/Commands&#039;);

    require base_path(&#039;routes/console.php&#039;);
}

}

评论 (0)

还没有评论,来做第一个吧。

发表评论

提交后需管理员审核通过后显示