类UserPolicy不存在(查看:F:\ xampp \ htdocs \ gates_policies \ resources \ views \ home.blade.php)

时间:2017-09-20 14:35:24

标签: php laravel-5.4

这是我的AuthServiceProvider文件,

<?php
namespace App\Providers;
use App\User;
use App\Policies\UserPolicy;
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',
    User::class => UserPolicy::class,
];

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

    Gate::define('display_msg','UserPolicy@display');
}
}

在我的刀片文件中,我试图展示“Hello World&#39;如果用户在Laravel中使用策略有效。

我的刀片文件代码:

@extends(&#39; layouts.app&#39)

@section('content')
<div class="container">
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">Dashboard</div>

            <div class="panel-body">
                @if (session('status'))
                    <div class="alert alert-success">
                        {{ session('status') }}
                    </div>
                @endif

                I am User<br>

                @can('display_msg')
                Hello World
                @endcan
            </div>
        </div>
    </div>
</div>
</div>
@endsection

UserPolicy文件中的代码:

<?php

namespace App\Policies;

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

class UserPolicy
{
use HandlesAuthorization;

/**
 * Create a new policy instance.
 *
 * @return void
 */
public function __construct()
{
    //
}

public function display(User $user)
{
    if($user->name == 'vrajesh')
    {
        return true;
    }
    else{
        return false;
    }
}
}

我收到错误: 类UserPolicy不存在(查看:F:\ xampp \ htdocs \ gates_policies \ resources \ views \ home.blade.php)

2 个答案:

答案 0 :(得分:1)

尝试更改:

Gate::define('display_msg','UserPolicy@display');

为:

Gate::define('display_msg','App\Policies\UserPolicy@display');

答案 1 :(得分:0)

在类似情况下(Laravel 5.4),我使用命令解决了问题

composer dump-autoload