首页 > 解决方案 > 使用自动绑定时策略类中的字符串或对象?

问题描述

我正在使用 Laravel 8。

我有这条路线:

Route::resource('organizations/{organization}/users/{user}/alerts', AlertController::class)->whereUuid(['organization', 'user', 'alert'])->scoped();

我像这样管理控制器中的策略:

public function __construct() {
    $this->organization = request()->route()->parameter('organization');
    $this->user = request()->route()->parameter('user');
    $this->alert = request()->route()->parameter('alert');
    Log::info($this->organization);
    Log::info($this->user);
    Log::info($this->alert);

    $this->authorizeResource(Alert::class);
}

如您所见,有一些调试行。在这一步中,所有路由参数都可以:我为每个参数设置了一个 uuid。

在策略类中:

public function __construct()
{
    $this->organization = request()->route()->parameter('organization');
    $this->user = request()->route()->parameter('user');
    $this->alert = request()->route()->parameter('alert');
}

public function before(User $user)
{
    if ($user->role_id === 'SUPERADMIN') {
        return true;
    }
    Log::info($this->organization);
    Log::info($this->user);
    Log::info($this->alert);

    if ($user->organization_id !== $this->organization->id) {
        return false;
    }
}

相同的调试行显示“组织”和“用户”的 uuid(字符串)。但“警报”的对象。

在这个策略类中,我必须拥有什么样的 var?字符串或相对于字符串的对象?

标签: laravel

解决方案


推荐阅读