php Laravel 5:如果用户已登录,则更改导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29333291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5: Change navbar if user is logged
提问by bockzior
I'm completely new to Laravel, MVC and templating engines in general.
总的来说,我对 Laravel、MVC 和模板引擎完全陌生。
I need to show certain navbar buttons and options if a user is logged in such as: Notifications, Logout, Profile, etc... and a Login button otherwise.
如果用户登录,我需要显示某些导航栏按钮和选项,例如:通知、注销、配置文件等......否则显示登录按钮。
Any help on how I could address this the right way is greatly appreciated. This is what I'm considering at the moment:
非常感谢我如何以正确的方式解决这个问题的任何帮助。这是我目前正在考虑的:
- A
User
object is always passed to the view. - The view checks if the
User
is set (meaning it's logged in) to include the appropriate partial blade template for the navbar.
- 甲
User
对象总是传递给视图。 - 该视图检查是否
User
已设置(意味着它已登录)为导航栏包含适当的部分刀片模板。
app.blade.php:
app.blade.php:
...
@if (isset($user))
@include('partials.navbarlogged')
@else
@include('partials.navbar')
...
Is this the best method? Thanks for your time!
这是最好的方法吗?谢谢你的时间!
回答by Jacob
If you are using Laravel 5's built in User model you can simply do
如果您使用 Laravel 5 的内置用户模型,您可以简单地执行
@if (Auth::check())
//show logged in navbar
@else
//show logged out navbar
@endif
回答by brianokinyi
For laravel 5.7 and above, use @auth and @guest directives.Here is the official documentation here
对于laravel 5.7 及以上版本,使用@auth 和@guest 指令。这里是官方文档
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
You may specify the authentication guard that should be checked when using the @auth
and @guest
directives:
您可以指定在使用@auth
and@guest
指令时应检查的身份验证防护 :
@auth('admin')
// The user is authenticated...
@endauth
@guest('admin')
// The user is not authenticated...
@endguest
Otherwise, you can use the @unless directive:
否则,您可以使用 @unless 指令:
@unless (Auth::check())
You are not signed in.
@endunless
回答by Zachary Schuessler
New versions of Laravel (5.6 at time of writing) support these two directives in Blade:
Laravel 的新版本(撰写本文时为 5.6)在 Blade 中支持这两个指令:
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
See official documentation here: https://laravel.com/docs/5.6/blade
在此处查看官方文档:https: //laravel.com/docs/5.6/blade
回答by Rohan Khude
You can also use Auth::guest()
你也可以使用 Auth::guest()
The Auth::guest()
method returns true or false.
该Auth::guest()
方法返回真或假。
Example -
例子 -
@if (Auth::guest())
<a href="{{ route('login') }}">Login</a>
<a href="{{ route('register') }}">Register</a>
@else
{{ Auth::user()->name }}
<a href="{{ route('logout') }}">Logout</a>
@endif
回答by Paulus Otto Harman
Starting from Laravel 5.4 there's new blade directive @includeWhen
which includes view based on a given boolean condition :
从 Laravel 5.4 开始,有新的刀片指令@includeWhen
,其中包括基于给定布尔条件的视图:
@includeWhen(Auth::check(), 'partials.navbarlogged')
@includeWhen(Auth::guest(), 'partials.navbar')
回答by bohemianjock
This should help :
这应该有帮助:
@extends(Auth::check() ? 'partials.navbarlogged' : 'partials.navbar')
The Auth::chek() sees whether the user is logged in or not and returns a boolean.
Auth::chek() 查看用户是否登录并返回一个布尔值。