Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php LARAVEL:获取管理仪表板中输出的计划任务数组_Php_Laravel_Laravel 5 - Fatal编程技术网

Php LARAVEL:获取管理仪表板中输出的计划任务数组

Php LARAVEL:获取管理仪表板中输出的计划任务数组,php,laravel,laravel-5,Php,Laravel,Laravel 5,使用Laravel任务调度器,我在Kernel.php中创建了许多任务 // Example function needs to be created $tasks = getAllScheduledTasks(); @foreach($tasks as $task) <tr> <td>{{ $task->name }}</td> <td>{{ $task->description }}<

使用Laravel任务调度器,我在Kernel.php中创建了许多任务

// Example function needs to be created
$tasks = getAllScheduledTasks();

@foreach($tasks as $task)
    <tr>
        <td>{{ $task->name }}</td>
        <td>{{ $task->description }}</td>
    </tr>
@endforeach
e、 g:

我想显示计划命令的列表和频率(以及上一次/下一次运行时间,我可以使用before/after函数记录自己的运行时间)

然而,我被困在了第一关。我不确定如何获得Kernel.php中定义的预定任务数组

// Example function needs to be created
$tasks = getAllScheduledTasks();

@foreach($tasks as $task)
    <tr>
        <td>{{ $task->name }}</td>
        <td>{{ $task->description }}</td>
    </tr>
@endforeach
//需要创建示例函数
$tasks=getAllScheduledTasks();
@foreach($tasks作为$task)
{{$task->name}
{{$task->description}
@endforeach

简化问题:如何在Laravel中获得一系列计划任务?

不幸的是,实际上并没有现成的支持。您需要做的是扩展
artisan schedule
命令并添加
列表
功能。谢天谢地,您可以运行一个简单的类:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;

class ScheduleList extends Command
{
    protected $signature = 'schedule:list';
    protected $description = 'List when scheduled commands are executed.';

    /**
     * @var Schedule
     */
    protected $schedule;

    /**
     * ScheduleList constructor.
     *
     * @param Schedule $schedule
     */
    public function __construct(Schedule $schedule)
    {
        parent::__construct();

        $this->schedule = $schedule;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $events = array_map(function ($event) {
            return [
                'cron' => $event->expression,
                'command' => static::fixupCommand($event->command),
            ];
        }, $this->schedule->events());

        $this->table(
            ['Cron', 'Command'],
            $events
        );
    }

    /**
     * If it's an artisan command, strip off the PHP
     *
     * @param $command
     * @return string
     */
    protected static function fixupCommand($command)
    {
        $parts = explode(' ', $command);
        if (count($parts) > 2 && $parts[1] === "'artisan'") {
            array_shift($parts);
        }

        return implode(' ', $parts);
    }
}
这将为您提供一个明细表命令列表


当然,别忘了注入
Facade
使用illumb\Support\Facades\Artisan

由于您没有通过控制台运行,您需要在控制器的内核上调用schedule方法。。。(不要忘记将
计划
方法公开,而不是保护)


我的任务是从管理仪表板停止/启用和编辑计划任务的频率。因此,我将它们放在Kernel.php中,并在函数中捕获输出

$enabled_commands = ConsoleCommand::where('is_enabled', 1)
        ->get();

    foreach ($enabled_commands as $command)
    {
        $schedule->command($command->command_name)
            ->{$command->frequency}($command->time)
            ->after(function () use ($command) {
                $this->log($command->command_name);
            });
    }

希望这能对你有所帮助。

以防有人像我一样在2021年尝试这样做。基于旧的答案和一点玩弄

在Laravel Framework 8.22.1中,我通过公开app/Console/Kernel->schedule方法,然后在控制器中工作:

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Events\Dispatcher;

    private function getScheduledJobs()
    {
        new \App\Console\Kernel(app(), new Dispatcher());
        $schedule = app(Schedule::class);
        $scheduledCommands = collect($schedule->events());

        return $scheduledCommands;
    }

希望这能为像我这样的未来谷歌用户节省时间。

感谢似乎走上了正确的轨道。但是,它返回的事件(illumb\Console\Scheduling\Event)似乎无法访问将包含$name、$description等的父命令类。是否有办法获取此信息?不,您无法获取此信息,因为Laravel通过运行
php artisan[Command]将其作为新命令执行
它不会奇怪地调用任何内部构件。您最好拥有一个共享配置,其中包含您所需的所有数据,您可以将这些数据分别读入计划方法和控制器。这不是一个坏主意,将其全部放入配置文件,然后从内核中调用它。PHP刚刚意识到,这似乎给出了一个重复的数组,每个计划项都列出两次,这有什么原因吗?我看不出在尝试遍历Artisan代码时发生了什么,您必须以某种方式在计划方法中添加两次;-)这在命令行上非常有效。。。但是当我通过Artisan::call('schedule:list')从控制器代码调用它时;我的退出代码似乎是0。我希望返回一个可以循环并显示在页面中的$tasks数组(甚至可以显示上面命令的简单字符串输出)。是否仍要实现此目的?编辑:通过调用Artisan::output(),将其存储在变量中,并显示在标记内的页面中,至少获得了输出。@armyofda12mnkeys我找到了一种将其获取为数组的方法:以下是一种将其获取为控制器数组的方法:
$enabled_commands = ConsoleCommand::where('is_enabled', 1)
        ->get();

    foreach ($enabled_commands as $command)
    {
        $schedule->command($command->command_name)
            ->{$command->frequency}($command->time)
            ->after(function () use ($command) {
                $this->log($command->command_name);
            });
    }
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Events\Dispatcher;

    private function getScheduledJobs()
    {
        new \App\Console\Kernel(app(), new Dispatcher());
        $schedule = app(Schedule::class);
        $scheduledCommands = collect($schedule->events());

        return $scheduledCommands;
    }