Laravel-如何每分钟运行一个功能/控制器?(任务计划) [英] Laravel - How to run a function/controller every minute ? (Task Scheduling)
问题描述
https://laravel.com/docs/5.4/scheduling
大家好,
我有一个控制器方法,我想每分钟运行一次.我阅读了任务计划的文档,但似乎只记录了命令.
I have a controller method that i want to run every minute . I read the documentation for Task Scheduling but it seems to document Commands only.
我有什么办法可以每分钟使用它来调用路由/控制器方法/脚本吗?
Is there any way that i can use it to call a route/controller method/script every minute ?
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DateTime;
use Illuminate\Support\Facades\DB;
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();
}
}
我需要使用数据库外观和外部类等...因此无法将其添加到内核方法中.
I need to use the DB facades and external classes etc... so its not possible to add it to the kernel method.
谢谢
推荐答案
是的,就像文档中所述的那样,您可以调用任何函数.因此,您可以执行以下操作:
Yes, just like the documentation states, you can make a call to any function. So you can do the following:
$schedule->call(function () {
$controller = new \App\Http\Controllers\ScriptController();
$controller->UpdateAcc();
})->everyMinute();
但是,从网络上下文中调用控制器是非常不习惯的做法,您应该创建外观或执行所需代码的作业.
However, it is very bad practice to call a controller out of its web context, you should create a facade or a job that executes the code you want.
这篇关于Laravel-如何每分钟运行一个功能/控制器?(任务计划)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!