gpt4 book ai didi

php - 使用 Laravel 任务调度请求 URL

转载 作者:行者123 更新时间:2023-12-02 15:16:21 25 4
gpt4 key购买 nike

环境

  • Laravel 版本:5.1.45 (LTS)

  • PHP 版本:5.6.1


描述

我试图每分钟都走一条特定的路线。


尝试

我试过了

$schedule->exec('curl '.env('APP_URL').'fbwifi/acl_update')->everyMinute()
->sendOutputTo(public_path().'/tasks/log.txt');

内核.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\App\Console\Commands\Inspire::class,
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->exec('curl '.env('APP_URL').'fbwifi/acl_update')->everyMinute()
->sendOutputTo(public_path().'/tasks/log.txt');
}
}

我所做的是获取路线的最佳方式吗?还是我应该研究更好的方法?

最佳答案

更新可能的解决方案:

如果您的 Controller 在默认命名空间中,那么您可以通过在调度的调用函数中使用完整的命名空间路径来使用它。

默认命名空间: App\Http\Controllers

假设 fbwifi 是您的 Controller ,acl_update 是您的方法。

$schedule->call('App\Http\Controllers\fbwifi@acl_update')
->everyMinute()
->sendOutputTo(public_path().'/tasks/log.txt');

我想遵循最佳实践,按照以下文章所述创建单独的类。

https://www.sitepoint.com/managing-cronjobs-with-laravel/

1) 创建命令类 -> 提供命令的签名和描述

    /**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sms:birthday';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description.';


2) 将您的 handle() 方法定义到该命令类中

public function handle()
{
User::whereBirthDate(date('m/d'))->get();

foreach( $users as $user ) {
if($user->has('cellphone')) {
SMS::to($user->cellphone)
->msg('Dear ' . $user->fname . ', I wish you a happy birthday!')
->send();
}
}

$this->info('The happy birthday messages were sent successfully!');

}


3) 在 ConsoleKernal 扩展类的 $commands 数组中添加该命令类

protected $commands = [
// ...
'App\Console\Command\HappyBirthday'
];


4) 根据您的要求安排您的命令,如下所示。

protected function schedule(Schedule $schedule)
{
$schedule->command('sms:birthday')->daily();
}

关于php - 使用 Laravel 任务调度请求 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40165841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com