当前位置:网站首页>laravel构造函数和中间件执行顺序问题
laravel构造函数和中间件执行顺序问题
2022-07-07 14:07:00 【全栈程序员站长】
今天想重构下代码结构:
BaseController.php 放置公共的中间件
class BaseController
{
public function __construct(){
$this->middleware('login');
}
}
IndexController.php 继承 BaseController
class IndexController extends BaseController
{
$user; // 存储登录用户实例
public function __construct(){
parent::__constuct();
$this->user = Auth::user();
}
}
--------------------
如上代码,一直报错!提示获取不到用户实例!
各种测试,kernel.php, route.php, BaseController, middleware写法,等等,找不到问题。甚至一度怀疑 parent::__contruct()继承不对...
最终发现,是构造函数和中间件的执行顺序有问题:
1.先执行构造函数
2.再调用中间件
所以,我的问题就是,login中间件要求用户登录,而构造函数又先执行了,直接获取用户信息,还未登录呢!
百度发现一篇文章,描述了这个问题:
https://laravel-china.org/topics/3218/laravel53-middleware-and-controller-construction-method-execution-sequence-problem
/*
它里面提到了一句:
不可在构造函数中,直接获取到session变量或认证后的用户实例!因为中间件还未启动!
这个描述的不准确!
在用户登录后,构造函数中,自然就获取到session变量或认证后的用户实例!
主要还是中间件未在之前执行!说白了就是顺序的问题!
*/
解决方法:
都只能在route.php 中了
1>要么使用 route::get()->middle('login')
2>要么使用 route::get(['middleware' => 'login', xxx])
最好的方法,应该是使用路由组 route::group(['middleware' => 'login'])
或者是:
抛弃在控制器中使用用户实例
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113214.html原文链接:https://javaforall.cn
边栏推荐
- Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
- It's different for rich people to buy a house
- What are compiled languages and interpreted languages?
- How to determine whether the checkbox in JS is selected
- 121. 买卖股票的最佳时机
- ThinkPHP URL 路由简介
- [flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- How to query the data of a certain day, a certain month, and a certain year in MySQL
- 【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
猜你喜欢
分步式监控平台zabbix
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
Application example of infinite list [uigridview]
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
Plate - forme de surveillance par étapes zabbix
Three. JS introductory learning notes 03: perspective projection camera
Mysql database basic operation DQL basic query
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
AB package details in unity (super detail, features, packaging, loading, manager)
Three. JS introductory learning notes 04: external model import - no material obj model
随机推荐
分类模型评价标准(performance measure)
Three. JS introductory learning notes 04: external model import - no material obj model
How to determine whether the checkbox in JS is selected
SPI master rx time out中断
How does geojson data merge the boundaries of regions?
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
Application example of infinite list [uigridview]
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Xcode Revoke certificate
Numpy --- basic learning notes
Unity3D_ Class fishing project, bullet rebound effect is achieved
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
Odoo集成Plausible埋码监控平台
Odoo integrated plausible embedded code monitoring platform
Three. JS introduction learning notes 12: the model moves along any trajectory line
iptables只允许指定ip地址访问指定端口
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
AE learning 01: AE complete project summary
You Yuxi, coming!