当前位置:网站首页>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
边栏推荐
- 分类模型评价标准(performance measure)
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- hellogolang
- Three. JS introductory learning notes 19: how to import FBX static model
- 深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
- 谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
- Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
- Use moviepy Editor clips videos and intercepts video clips in batches
- Detailed explanation of unity hot update knowledge points and introduction to common solution principles
- 星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
猜你喜欢

Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc

What are compiled languages and interpreted languages?
![[wechat applet] Chapter (5): basic API interface of wechat applet](/img/63/e819113c7c93e273525bc850892972.png)
[wechat applet] Chapter (5): basic API interface of wechat applet

Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?

Three. JS introductory learning notes 11:three JS group composite object

Odoo integrated plausible embedded code monitoring platform

Dotween -- ease function

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition

AE learning 02: timeline
随机推荐
分步式监控平台zabbix
Numpy -- data cleaning
C4D learning notes 1- animation - animation key frames
Unity3d click events added to 3D objects in the scene
Shader_ Animation sequence frame
Use moviepy Editor clips videos and intercepts video clips in batches
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
神经网络c语言中的指针是怎么回事
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
保证接口数据安全的10种方案
What are compiled languages and interpreted languages?
iptables只允许指定ip地址访问指定端口
持续创作,还得靠它!
[wechat applet] Chapter (5): basic API interface of wechat applet
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
分步式監控平臺zabbix
Laravel 中config的用法
You Yuxi, coming!
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
three.js打造酷炫下雪效果