php - 无法在 Laravel 5.4 的 Blade 模板中使用 Auth::user()

标签 php laravel authentication laravel-5.4

我想用 guestauth 函数制作我的索引页。

客人来访时,可以尝试登录;登录后,它会显示用户的信息,例如用户名。

下面是我做的。

路由在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/

相关文章:

linux - 编写 Bash 脚本以在登录时出错时注销。需要指导

php - 表连接 SQL/PHP (CRUD)

php - 表情符号到 JSON 编码,发布到网络服务器

php - API 平台 - UI 服务器端点定义

php - Laravel 4 同一张表的多个 hasmany

linux - 没有任何身份验证的 SSH session

c# - 登录前的表单例份验证登陆页面

javascript - 页面不在 Wordpress 中加载样式表或 JS 文件

laravel - 更改 "password reset "url 链接

php - 如何在 Laravel 中使用两个WhereIn