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

标签 php laravel curl laravel-5

环境

  • 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/

相关文章:

python - 如何展平 Pandascore E-Sports GET 响应格式的 JSON 数据,以便在 pandas 中拥有非常干净的数据框?

python - Python 请求中的 CURL -T(--upload-file 等效项)

php - 从 MYSQL 表中删除值

php - Cookie 会在服务器 IP 更改时过期吗?

PHP 在 <a - 标签之前添加空格

php - 拉维尔 5 : How to group a collection dynamically by one column or another depending on item's column value?

rest - 解析 REST API 将 ACL 设置为对象

php - 搜索包含需要放入数组的字段的查询

database - 使用 Laravel/Eloquent 从复制行中检索对象

mysql - 连接被拒绝 - Laravel,远程数据库