类App \ Policies \ StatusPolicy不存在

时间:2018-07-08 18:41:36

标签: php laravel laravel-5

我设置了课程,因此可以使用Laravel Authorization并使用“策略”功能。但是在为我的方法定义中间件时,我总是收到此错误(类App \ Policies \ StatusPolicy不存在)。这就是我所拥有的:

AuthServiceProvider.php

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\Status' => 'App\Policies\StatusPolicy',
    ];

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

        //
    }
}

StatusController.php

namespace App\Http\Controllers;

use App\Status;
use Illuminate\Http\Request;

class StatusController extends Controller
{

    public function __construct()
    {
         $this->middleware('can:list,App\Status')->only('index');
         $this->middleware('can:update,status')->only('edit');
    }

    // ...

StatusPolicy.php(由php artisan make生成:policy StatusPolicy --model = Status

namespace Policies;

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

class StatusPolicy
{
    use HandlesAuthorization;

    /**
     * Verifica se o usuário tem permissão para listar os status.
     *
     * @param  \App\User  $user
     * @return bool
     */
    public function list(User $user)
    {
        return true;
    }

    /**
     * Determine whether the user can view the status.
     *
     * @param  \App\User  $user
     * @param  \App\Status  $status
     * @return mixed
     */
    public function view(User $user, Status $status)
    {
        //
    }

    /**
     * Determine whether the user can create statuses.
     *
     * @param  \App\User  $user
     * @return mixed
     */
    public function create(User $user)
    {
        //
    }

    /**
     * Determine whether the user can update the status.
     *
     * @param  \App\User  $user
     * @param  \App\Status  $status
     * @return mixed
     */
    public function update(User $user, Status $status)
    {
        return true;
    }

    /**
     * Determine whether the user can delete the status.
     *
     * @param  \App\User  $user
     * @param  \App\Status  $status
     * @return mixed
     */
    public function delete(User $user, Status $status)
    {
        //
    }
}

2 个答案:

答案 0 :(得分:0)

我发现了问题。

由于某种原因,命令php artisan make:policy创建了一个名称空间错误的文件。解决方法是更改​​StatusPolicy.php文件中的名称空间:

来自namespace Policies;namespace App\Policies;

答案 1 :(得分:0)

将您的StatusPolicy类中的命名空间更改为App \ Policies;