错误页面中的Laravel Auth [英] Laravel Auth in error pages

查看:74
本文介绍了错误页面中的Laravel Auth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户登录到我的应用程序时,导航中将出现一个下拉列表,以访问管理.但是,当用户偶然发现错误页面(例如404)时,不会显示他们正在登录.而是显示登录".为什么会这样?

When a user is logged into my application, there is a dropdown in the navigation to access administration. However, when a user stumbles across an error page (404, for instance) it doesn't show them being logged in. Instead it shows "Login". Why is this?

这是我的代码.

@if (Auth::check())

    <ul class="nav navbar-nav navbar-right">
        <li class="dropdown">
            <a style="cursor: pointer;" class="dropdown-toggle" data-toggle="dropdown"><i class="glyphicon glyphicon-user"></i> {{ Auth::user()->name }} <b class="caret"></b></a>
            <ul class="dropdown-menu">
                <li>
                    <a href="/admin/dashboard"><i class="glyphicon glyphicon-tasks"></i> Dashboard</a>
                </li>
                <li role="separator" class="divider"></li>
                <li><a href="/logout"><i class="glyphicon glyphicon-log-out"></i> Logout</a></li>
            </ul>
        </li>
    </ul>

@else

    <ul class="nav navbar-nav navbar-right">
        <li><a href="/login"><i class="glyphicon glyphicon-log-in"></i> Login</a</li>
    </ul>

@endif

推荐答案

Laravel似乎仅在其认为合适的会话(不是404页面)中以 https://github .com/laravel/framework/issues/11653 .

It looks like Laravel only starts the session where it sees appropriate (which isn't a 404 page) as Daniel pointed out: https://github.com/laravel/framework/issues/11653.

为了解决这个问题(导航栏是站点范围内的,包括404页),我在全局中间件中添加了StartSession类.

To combat this (as the navigation bar is site-wide, including 404 pages) I added the StartSession class to the global middlwares.

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class
];

现在变成...

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Session\Middleware\StartSession::class
];

这篇关于错误页面中的Laravel Auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆