Laravel无法查看用户是否在404错误页面上登录 [英] Laravel can't see if a user is logged in on 404 error page

查看:64
本文介绍了Laravel无法查看用户是否在404错误页面上登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个自定义的404错误页面,并且正在使用我的app.layout对其进行包装.

I have created a custom 404 error page and I am using my app.layout to wrap it.

但这很奇怪,因为即使我登录了Login和Register链接,当我得到404错误页面时,也会看到.为什么会这样?

But it's strange because when i get 404 error page even though I'm logged in the Login and Register links are seen. Why is that?

当我登录页面并编写不存在的路线时,我得到了具有扩展布局的404页面,但是Laravel似乎无权访问Auth :: class并且它看不到用户是否已登录是否.

when i log in on my page and write non existing route i get my 404 page with extended layout but it seems like Laravel doesn't have access to Auth::class and it doesn't see if a user is logged in or not.

因此,我没有看到注销"链接,而是看到了登录"和注册"链接

So instead of seeing Logout link i see "Login" and "Register" links

推荐答案

检查您的app.layout是否包含导航部分.

Check if your app.layout included a nav partial.

这样的布局html:

<div class="content-wrapper">
 <!-- Content Header (Page header) -->
 @include('partials.crumble_head')
 <!-- Main content -->
 <section class="content">
  @yield('content')
 </section>
 <!-- /.content -->
</div>

如果您的内容页面扩展了此布局,则partial不会消失

if your content page extends this layout, then partial will not disapear

答案需要根据我们的讨论进行更新.

Open app/Http/Kernel.php. Add below block to the $middleware

    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,

所以您的代码应该这样:

so your code should like this:

/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

现在,您应该可以在views/errors/404.blade页面中访问Auth.确保您在Auth之前输入"\".例如:

Now you should be able to access Auth in your views/errors/404.blade page. Make sure you put a '\ ' before Auth. For Eg:

{{\Auth::user()->name}}

https://laracasts.com/discuss/channel/laravel/getting-authuser-on-custom-404-page

这篇关于Laravel无法查看用户是否在404错误页面上登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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