本文介绍了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运行方法内部的内容
发布评论