多模块中Command调度

Modules/模块名/Console 下的 类文件只要继承Illuminate\Console\Command 类就会被作为artisan Command命令被自动加载
例如本demo 中的 Modules\Test\Console\TestCommand.php

创建命令

在 Test 模块下 创建一个名为TestCommand的artisan命令
php artisan module:make-command TestCommand Test

demo

<?php

namespace Modules\Test\Console;

use Illuminate\Console\Command;
use Modules\Test\Entities\Test;

class TestCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $name = 'command:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command 任务测试';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $test = new Test();
        $test->fill([
            'title'   => '测试每6小时任务调度', // 'App req=>' . date('Y-m-d H:i:s'),
            'content' => date('Y-m-d H:i:s'),
        ]);
        $test->save();
    }
}

调度

php artisan command:test

或者

// 每 6小时 执行一次测试任务
$schedule->command('command:test')->everySixHours();