Laravel授权可以访客

问题描述 投票:1回答:2

如果没有登录,我正在尝试授权访客显示登录表单。

@can('guest')
    <a href="/login">Let's Start</a>
@endcan 

我在哪里需要定义客人

laravel
2个回答
3
投票

默认情况下,Blade模板引擎提供所谓的authentication directives,用于确定用户是否经过身份验证:

@auth
    // User is authenticated.
@endauth

@guest
    // User is not authenticated.
@endguest

1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.