我想用 guest
和 auth
函数制作我的索引页。
客人来访时,可以尝试登录;登录后,它会显示用户的信息,例如用户名。
下面是我做的。
路由在routes/web.php
中定义:
Route::get('/', 'CommonController@index');
Auth::routes();
在 index
的 blade 模板中,我使用了 Auth::user()
来验证身份验证但失败了。
@if(Auth::user())
<div id="user-panel">
<a href="" class="user-panel-button">
New Article
</a>
<div class="btn-group user-panel-button">
<a type="button" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="{{ Auth::logout() }}">Logout</a></li>
</ul>
</div>
</div>
@else
<a href="{{ url('login') }}" class="button">
Login
</a>
@endif
每次我成功登录后,它仍然会显示登录
按钮而不是用户名按钮,在我刷新索引页面后。
我查看了 session 信息,一旦我刷新页面,login_web_xxxxxxxxxxx
session 将消失。
此外,当我为 auth /home
使用 Laravel 默认路径时,它起作用了。无论您刷新页面多少次,用户名始终可以显示。
我该如何解决这个问题?非常感谢。
最佳答案
对于 Laravel 5.4,您可以使用:
@if(Auth::check())
// The user is authenticated...
@else
// The user is not authenticated...
@endif
对于 Laravel 5.5 及以上版本:
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
关于php - 无法在 Laravel 5.4 的 Blade 模板中使用 Auth::user(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45088520/