无法在 Laravel 5.4 的刀片模板中使用 Auth::user()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45088520/
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
Cannot use Auth::user() in blade template in Laravel 5.4
提问by Qilei Ren
I want to make my index page with both guest
and auth
functions.
我想让我的索引页同时具有guest
和auth
功能。
When guests visit, they could try logging in; And after login, it will show the user's info such as username.
当客人来访时,他们可以尝试登录;并且在登录后,它会显示用户的信息,如用户名。
Below is what I did.
下面是我所做的。
Routes are defined in routes/web.php
:
路由定义在routes/web.php
:
Route::get('/', 'CommonController@index');
Auth::routes();
In blade template of index
, I used Auth::user()
to validate authentication but failed.
在刀片模板中index
,我曾经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
Every time I logged in successfully, it will still show login
button instead of user name button, after I REFRESH the index page.
每次我成功登录login
后,在我刷新索引页面后,它仍然会显示按钮而不是用户名按钮。
I have checked session information, once I refreshed the page, the login_web_xxxxxxxxxxx
session will be disappeared.
我已经检查过会话信息,一旦我刷新页面,login_web_xxxxxxxxxxx
会话就会消失。
Furthermore, When I use Laravel default path for auth /home
, it worked. Username could always be presented whatever how many times you refresh the page.
此外,当我为 auth 使用 Laravel 默认路径时/home
,它起作用了。无论您刷新页面多少次,都可以始终显示用户名。
How should I fix this issue? Many thanks.
我应该如何解决这个问题?非常感谢。
回答by omarjebari
For Laravel 5.4 you can use:
对于 Laravel 5.4,您可以使用:
@if(Auth::check())
// The user is authenticated...
@else
// The user is not authenticated...
@endif
For Laravel 5.5 and above:
对于 Laravel 5.5 及以上版本:
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
回答by Qilei Ren
Instead of using
而不是使用
<ul class="dropdown-menu">
<li><a href="{{ Auth::logout() }}">Logout</a></li>
</ul>
For log out, it is better to be codes as below, which is provided by official resources/view/layouts/app.php
登出最好是下面的代码,官方提供 resources/view/layouts/app.php
<ul class="dropdown-menu">
<li>
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
回答by Abdullah Al Shakib
Update routes/web.php
file as
将routes/web.php
文件更新为
Route::group(['middleware' => 'web'], function () {
Route::get('/', 'CommonController@index');
Auth::routes();
}
Now to check auth in your blade template, you can use
现在要在您的刀片模板中检查身份验证,您可以使用
@if(\Auth::check())
....
....
@else
....
....
@endif
回答by iMezied
also you can use the full class path of Auth in blade file like this
您也可以像这样在刀片文件中使用 Auth 的完整类路径
@if(\Illuminate\Support\Facades\Auth::check())
// code here
@endif