如果没有登录,我正在尝试授权访客显示登录表单。
@can('guest')
<a href="/login">Let's Start</a>
@endcan
我在哪里需要定义客人
默认情况下,Blade模板引擎提供所谓的authentication directives
,用于确定用户是否经过身份验证:
@auth
// User is authenticated.
@endauth
@guest
// User is not authenticated.
@endguest
prd的答案是一个非常好的方法,并将在更新的laravel版本中工作。 (可能来自5.5)
但是,如果您仍想使用can('guest')
格式,则可以这样做。
您需要将自定义刀片指令放在AppServiceProvider.php
文件和boot
方法中。
Blade::if('can', function ($role) {
if($role=='guest'){
return !(auth()->check());
}
// you can also put codes for can('admin') etc
// the return value should be true or false.
});
有关更多信息,请阅读this。