这是我的身份验证服务提供程序文件,
<?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",前提是用户使用Laravel中的策略有效。
我的Blade文件的代码:
@extends("布局.应用程序")
@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不存在(查看:文件夹:\xampp\htdocs\网关策略\资源\视图\主页. blade.php)
2条答案
按热度按时间8ehkhllq1#
我使用命令解决了类似情况(Laravel 5.4)
这个命令强制composer浏览已安装的软件包并重新创建vendor\autoload.php文件(以及其他一些与自动加载类相关的文件)
1tu0hz3e2#
尝试更改:
致: