I have a package that contains Artisan commands. I’ve registered these commands with Artisan via my service provider like so:

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    // Register Amazon Artisan commands
    $this->commands([
        'App\Marketplace\Amazon\Console\PostProductData',
        'App\Marketplace\Amazon\Console\PostProductImages',
        'App\Marketplace\Amazon\Console\PostProductInventory',
        'App\Marketplace\Amazon\Console\PostProductPricing',
    ]);
}

但是,这些命令需要安排为每天运行.

I know in app/Console/Kernel.php there is the schedule() method where you can register commands and their frequency, but how can I schedule commands in my package’s service provider instead?

推荐答案

通过大量的调试和阅读Laravel的源代码才能弄清楚这一点,但事实证明它相当简单.诀窍是等到应用程序启动后才调度命令,因为这是Laravel定义Schedule实例,然后在内部调度命令的时候.希望这能为某些人节省几个小时的痛苦调试时间!

use Illuminate\Support\ServiceProvider;
use Illuminate\Console\Scheduling\Schedule;

class ScheduleServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->booted(function () {
            $schedule = $this->app->make(Schedule::class);
            $schedule->command('some:command')->everyMinute();
        });
    }

    public function register()
    {
    }
}

Laravel相关问答推荐

如何使用 laravel 更新单个值

运行Docker时,安装PHP8.1时返回错误

在 laravel 中查询此类数据的最佳做法是什么?

Laravel Valet 安装后 Ping test.dev 返回未知主机

Laravel:如何强制使用 HTTPS?

Laravel 5 - ErrorException 无法打开流:权限被拒绝

laravel 5中的配置缓存导致找不到视图

使用 vue-router 更改路由时中止所有 Axios 请求

调用未定义的函数 mb_strimwidth

如何利用 StorageFacade 的 Filesystem 类的 glob 方法?

Laravel Eloquent 模型中的字段

Laravel 存储链接不适用于生产

如何在 Laravel 4 中组织不同版本的 REST API 控制器?

laravel 如何读取 app/config/app.php 调试变量

Laravel 5 - 从控制器级别的所有请求对象中删除参数

日期时间格式无效:1366 字符串值不正确

Laravel 5 - 跳过迁移

复合唯一密钥验证 - laravel

laravel - 无法在控制器构造函数中获取会话

如何使用队列设置高、低和中优先级Electron邮件?