Laravel 9 - RateLimiter 无法访问作业中受保护的属性

发布于 2025-01-17 20:05:43 字数 2508 浏览 2 评论 0原文

我一直在尝试使用 Laravel 9 对作业进行排队,并为我要使用的 API 设置速率限制。限制为每分钟最多 10 个请求。

我的 AppServiceProvider.php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register() {}

    public function boot()
    {
        RateLimiter::for('sendContentStackToEasyTranslate', function($job) {
            return Limit::perMinute(10)->by($job->entry->id);
        });
    }
}

我的 SenCSToET.php 作业中的中间件和构造函数

namespace App\Jobs;

use App\Models\ContentStackEntry;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\RateLimited;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use IncrediblePony\Auditlog\Traits\AuditlogTrait;

class SendCSToET implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, AuditlogTrait;

    /**
     * @var \App\Models\ContentStackEntry
     */
    protected $entry;

    /**
     * Create a new job instance.
     *
     * @param \App\Models\ContentStackEntry
     * @return void
     */
    public function __construct(ContentStackEntry $entry)
    {
        $this->entry = $entry;
    }

    /**
     * Get the middleware the job should pass through.
     *
     * @return array
     */
    public function middleware() {
        return [new RateLimited('sendContentStackToEasyTranslate')];
    }
}

https://laravel.com/docs/9.x/queues#rate-limiting 是我这种方法的来源。

当代码被点击时,错误出现在我面前:

{
    "message": "Cannot access protected property App\\Jobs\\SendCSToET::$entry",
    "exception": "Error",
    "file": "/var/www/services/test-translation/releases/6/app/Providers/AppServiceProvider.php",
    "line": 30,
}

BUT如果我从更改SendCSToET.php文件中的$entry变量protectedpublic 代码按预期运行。我缺少什么?

I have been trying to queue a job with Laravel 9 and setting a rate limit, to the API I am going to hit. The limit is max 10 requests per minute.

My AppServiceProvider.php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register() {}

    public function boot()
    {
        RateLimiter::for('sendContentStackToEasyTranslate', function($job) {
            return Limit::perMinute(10)->by($job->entry->id);
        });
    }
}

My middleware and constructor in SenCSToET.php job

namespace App\Jobs;

use App\Models\ContentStackEntry;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\RateLimited;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use IncrediblePony\Auditlog\Traits\AuditlogTrait;

class SendCSToET implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, AuditlogTrait;

    /**
     * @var \App\Models\ContentStackEntry
     */
    protected $entry;

    /**
     * Create a new job instance.
     *
     * @param \App\Models\ContentStackEntry
     * @return void
     */
    public function __construct(ContentStackEntry $entry)
    {
        $this->entry = $entry;
    }

    /**
     * Get the middleware the job should pass through.
     *
     * @return array
     */
    public function middleware() {
        return [new RateLimited('sendContentStackToEasyTranslate')];
    }
}

https://laravel.com/docs/9.x/queues#rate-limiting is my source of this approach.

When the code is hit the error is presented to me:

{
    "message": "Cannot access protected property App\\Jobs\\SendCSToET::$entry",
    "exception": "Error",
    "file": "/var/www/services/test-translation/releases/6/app/Providers/AppServiceProvider.php",
    "line": 30,
}

BUT if I change the $entry variable in SendCSToET.php file from protected to public the code runs as expected. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2025-01-24 20:05:43

这是一个简单的PHP行为,您的RateLimiter的回调不属于sendcstoet类或任何子类,因此它只能访问公共属性/方法。因此,您必须在public属性或受保护 + public getter(清洁方法)之间进行选择。

sendcstoet.php中的示例getter函数:

protected $entry;

public function getEntry() {
  return $this->entry;
}

It's a simple PHP behaviour, the callback from your RateLimiter does not belong to SendCSToET class or any subclass, so it can only access public properties/methods. So you have to choose between a public property or protected + public getter (cleaner way).

Example getter function in SendCSToEt.php:

protected $entry;

public function getEntry() {
  return $this->entry;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文