Laravel自定义错误页面中的访问会话变量 [英] Access Session Variable in Laravel Custom Error Pages

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

问题描述

我正在尝试在Laravel 5.3中创建一个自定义错误页面.我想要检索存储在 Session 中的某些变量,例如 Session :: get("username"),但是我得到的是一个空变量.

I am trying to create a custom error page in Laravel 5.3. I wanted the retrieve some variable stored in the Session like Session::get("username"), however what I get is an empty variable.

我应该如何在Laravel的自定义错误页面中获取任何 Session 变量,还是不可能?

How am I suppose to get any Session variable in a custom error page in Laravel or is it not possible?

推荐答案

由于Laravel找不到路由,因此您没有通过中间件 \ Illuminate \ Session \ Middleware \ StartSession (即默认情况下位于 web 中间件组中),因此不会启动会话.

As Laravel did not find the route, you did not pass through the middleware \Illuminate\Session\Middleware\StartSession (which is by default in web middleware group), so your session is not started.

要初始化会话,请将 \ Illuminate \ Session \ Middleware \ StartSession :: class web 移到 $ middleware 中,code> Kernel.php :

To have your session initialized, move \Illuminate\Session\Middleware\StartSession::class, from web to $middleware in Kernel.php:

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

$ midleware 中的中间件始终运行,因此即使未找到路由,您的会话也会被初始化.

Middlewares in $midleware are always run so your session will be initialized, even if the route is not found.

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

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