Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel

x33g5p2x  于2022-07-04 转载在 其他  
字(1.2k)|赞(0)|评价(0)|浏览(244)

Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel

这个是我在阅读Laravel8中的文档时遇到的。在此阅读下@auth和@guest的用法。

下面将说明@auth和@guest在Laravel中的使用。

这两个关键字其实是代替@if、@endif的。

如下使用@if、@endif

@if(auth()->user())
 // The user is authenticated.
@endif

当用户有权限,就在blade中显示。

使用@auth和@guest可以简化成这样的:

@auth
 // The user is authenticated.
@endauth

@guest
 // The user is not authenticated.
@endguest

在个人项目中,我是这样用的

<nav class="navbar navbar-dark bg-dark" style="z-index:999">
    <div class="container-fluid">
        <a class="navbar-brand" href="/">it1995.cn</a>
        <ul class="navbar-nav justify-content-end">
            @guest
                <li class="nav-item">
                    <a class="nav-link text-white" href="{{route('login')}}">登录</a>
                </li>
            @else
                <li class="nav-item">
                    <div class="row">
                        <div class="col">
                            <a class="nav-link text-white" href="{{route('dashboard')}}">管理</a>
                        </div>
                        <div class="col">
                            <a class="nav-link text-white" href="{{route('signOut')}}">退出</a>
                        </div>
                    </div>
                </li>
            @endguest
        </ul>
    </div>
</nav>

相关文章

微信公众号

最新文章

更多