gpt4 book ai didi

php - 拉维尔 5.5 : Authorization Policy AccessDeniedHttpException This action is unauthorized

转载 作者:可可西里 更新时间:2023-11-01 00:54:21 26 4
gpt4 key购买 nike

我创建了一个授权策略,所以我遇到了这个问题。

我已经看到了这些解决方案,但我的问题还没有解决:

Solution 1

Solution 2

Solution 3

代码如下:

ArticalesController 类中使用的函数:

public function show(Articale $articale)
{
$this->authorize('view', $articale);
return view('articales.show',compact('articale'));
}

ArticalePolicy 类:

        <?php

namespace App\Policies;

use App\User;
use App\Articale;
use Illuminate\Auth\Access\HandlesAuthorization;

class ArticalePolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can view the articale.
*
* @param \App\User $user
* @param \App\Articale $articale
* @return mixed
*/
public function view(User $user, Articale $articale)
{
return $user->id == $articale->user_id;
}

AuthServiceProvider 类:

namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Articale::class => ArticalePolicy::class,
];

/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();

//
}
}

最佳答案

尝试使用您的模型名称,将其替换为您的 AuthServiceProvider

替换

Articale::class => ArticalePolicy::class,

'App\Articale' => 'App\Policies\ArticalePolicy',

关于php - 拉维尔 5.5 : Authorization Policy AccessDeniedHttpException This action is unauthorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53589500/

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