当前位置:网站首页>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
边栏推荐
- MySQL数据库基本操作-DQL-基本查询
- Summary of knowledge points of xlua hot update solution
- Three. JS introductory learning notes 03: perspective projection camera
- Syntaxhighlight highlights the right scroll bar
- Simple understanding and application of TS generics
- Three singleton modes of unity (hungry man, lazy man, monobehavior)
- Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
- numpy--疫情数据分析案例
- Learn good-looking custom scroll bars in 1 minute
- Regular expression string
猜你喜欢
How does geojson data merge the boundaries of regions?
Logback日志框架第三方jar包 免费获取
SPI master RX time out interrupt
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
Numpy -- epidemic data analysis case
Power of leetcode-231-2
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
MySQL数据库基本操作-DQL-基本查询
通知Notification使用全解析
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
随机推荐
[wechat applet] Chapter (5): basic API interface of wechat applet
Simple understanding and application of TS generics
A wave of open source notebooks is coming
Use moviepy Editor clips videos and intercepts video clips in batches
Xcode Revoke certificate
Laravel 中config的用法
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
Vite path alias @ configuration
Mysql database basic operation DQL basic query
Particle effect for ugui
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
Logback logging framework third-party jar package is available for free
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
Shader_ Animation sequence frame
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
asyncio 概念和用法
Three. JS introduction learning notes 12: the model moves along any trajectory line