我正在使用 Laravel 5.2。所以我正在学习如何处理角色和权限Authorization。一切运行良好。我什至制定了自己的政策 PostPolicy。
现在解决问题。我将 $post 数据加载到 PostsController 中的视图中,然后加载到刀片中。
帖子控制器:
public function show($id)
{
$post = Post::find($id);
return view('posts.show', compact('post'));
}
帖子/show.blade.php:
@section('content')
<!-- begin -->
@can('hasRole', Auth::user())
<h1>Displaying Admin content</h1>
@endcan
@can('hasRole', Auth::user())
<h1>Displaying moderator content</h1>
@endcan
@can('hasRole', Auth::user())
<h1>Displaying guest content</h1>
@endcan
政策:
public function hasRole($user)
{
// just for test
return true;
}
现在返回所有内容。
当我将 @can('hasRole', Auth::user())
Auth::user() 更改为字符串时,iE
@can('hasRole', 'guest')
<h1>Displaying guest content</h1>
@endcan
在这种情况下,它不会返回任何东西。由于我是 Laravel 的新手,我真的不知道它不起作用。