Laravel Cronjob运行方法内部的内容

编程入门 行业动态 更新时间:2024-10-24 16:34:03
本文介绍了Laravel Cronjob运行方法内部的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的控制器中,我有以下方法可以检查网址状态

In my controller i have these method where it checks for url status

public function index(){ $url = Url::latest()->paginate(); return UrlsResource::collection($url); }

我的UrlsResource返回这样的数据

public function toArray($request) { return [ 'id' => $this->id, 'url' => $this->url, 'status' => $this->status, 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d'), ]; }

$this->status中的位置是我的模型的访问器.

where in the $this->status is an accessor from my model.

public function getStatusAttribute(){ $handle = curl_init($this->url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($handle); $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); curl_close($handle); $message = [ '100' => '(100) Continue '. $this->url, '101' => '(101) Switching Protocols '. $this->url, '102' => '(102) Processing '. $this->url, ]; return $message[$httpCode]; }

如何运行cronjob,以便在其中运行索引方法中的内容?

How can I run a cronjob where in I want to run what's inside my index method?

我试图在其中输入命令

class CheckUrl extends Command { protected $signature = 'check:url'; protected $description = 'Check url'; public function __construct() { parent::__construct(); } public function handle() { $url = Url::latest()->paginate(); UrlsResource::collection($url); } }

我试图通过命令行运行它

and i tried to run it via command line

php artisan check:url

但什么也没发生

在将此cron条目添加到服务器的文档中说

it says on the documentation that add this cron entry to server

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

但是我不知道确切的位置(我使用xampp)

but i dont know exactly where to put it (im using xampp)

推荐答案

您可以将controller @ method作为字符串传递给call().

You can just pass the controller@method as a string to call().

$schedule->call('App\Http\Controllers\MyController@MyAction')->everyMinute();

更多推荐

Laravel Cronjob运行方法内部的内容

本文发布于:2023-07-16 04:11:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   内容   Laravel   Cronjob

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!