本文介绍了Laravel 5:在控制台内核的schedule()函数中使用Cache ::或DB ::的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Laravel 5中以用户配置的不同时间间隔运行Artisan控制台命令.我已经构建了控制台命令,并拥有一个数据库(使用Eloquent模型),该数据库具有运行频率"配置值.

I'm trying to run an Artisan console command in Laravel 5 at varying, user-configured time intervals. I have built the console command and have a database (with Eloquent model) holding a "run frequency" configuration value.

App\Console\Kernel.phpschedule()函数中,我想将此值从数据库中拉出,并根据需要根据需要在适当的时候运行命令.

Within the schedule() function of App\Console\Kernel.php I'd like to pull this value out of the database and run the command as and when needed based on this value.

使用带有或不带有use DB;DB::table()...时,我会收到一个Fatal error: Class 'DB' not found,如果我也尝试使用Cache::...函数,也会发生同样的情况.

I receive a Fatal error: Class 'DB' not found when using DB::table()... with or without use DB;, and the same happens if I try to use Cache::... functions too.

我假设无法从App\Console\Kernel.php文件的schedule()函数中访问IoC容器.有没有人有类似的发现,并且找到了一个优雅的解决方法?

I'm assuming the IoC container isn't accessible from within the schedule() function of App\Console\Kernel.php file. Has anyone had similar and found an elegant workaround?

我的简化代码是:

<?php namespace App\Console;

use Cache;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $settings = Cache::get('settings');
    }

和错误:

php artisan schedule:run
PHP Fatal error:  Class 'Cache' not found in /home/vagrant/projects/project/dir/api/app/Console/Kernel.php on line 26

任何帮助将不胜感激

推荐答案

Taylor在5.0.21中已解决此问题( Git Commit ),因此遇到相同问题的任何人都可以运行composer update获取该框架的最新版本,该版本将允许您使用

This has been fixed by Taylor in 5.0.21 (Git Commit) so anyone experiencing the same issue can run composer update to get the latest version of the framework which will allow you to use

Use DB;

Use Cache;

与往常一样

这篇关于Laravel 5:在控制台内核的schedule()函数中使用Cache ::或DB ::的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 09:16