php - Laravel - 如何每分钟运行一个函数/ Controller ? (任务调度)

我有一个 Controller 方法,我想每分钟运行一次。我读了 documentation用于任务调度,但它似乎只记录命令。

有什么方法可以让我每分钟调用一个路由/ Controller 方法/脚本吗?

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Acc;

class ScriptController extends Controller
{
    public function updateAcc()
    {
        $c = new Scripts\AccountA();
        $xoo = $c->getInfo();
        $z = json_decode($xoo,true);

        $data= new Acc();
        $data->price = $z["result"];
        $data->save();
    }
}

我需要使用DB facades外部类 等...

最佳答案

是的,正如文档所述,您可以调用任何函数。因此,您可以执行以下操作:

$schedule->call(function () {
    $controller = new \App\Http\Controllers\ScriptController();
    $controller->UpdateAcc();
})->everyMinute();

但是,从其 Web 上下文调用 Controller 是非常糟糕的做法,您应该创建一个 facade 或一个作业来执行您想要的代码。

https://stackoverflow.com/questions/46075200/

相关文章:

angularjs - ngrx 参数选择函数

node.js - 如何在 Node.js 中设置文件编码

javascript - 如何在 Gatsby.js 中将时间戳附加到 markdown 前面的内容

amazon-web-services - aws s3 ls 递归 grep 扩展 '.mov'

r - 使用 ggplot 的条形图重新排序无法正常工作

apache-spark-sql - pySpark groupby 中的条件聚合

scala - 为什么我不能在 Scala 中增加?

python - 无法将字节连接到 str(转换为 Python3)

php - 为什么 Google_Service_Drive 文件的属性只返回 null?

python - Python 中的 str.join() 和 str().join() 有什么区别