当前位置:网站首页>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
边栏推荐
- Xcode Revoke certificate
- Unity drawing plug-in = = [support the update of the original atlas]
- Three singleton modes of unity (hungry man, lazy man, monobehavior)
- What are compiled languages and interpreted languages?
- Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
- 2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- Unity3d click events added to 3D objects in the scene
- Three. JS introductory learning notes 15: threejs frame animation module
- Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
猜你喜欢

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

统计学习方法——感知机

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform

Numpy -- epidemic data analysis case

C4D learning notes 2- animation - timeline and time function

What are compiled languages and interpreted languages?

Three. JS introductory learning notes 03: perspective projection camera

Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import

无线传感器网络--ZigBee和6LoWPAN
随机推荐
Sysom case analysis: where is the missing memory| Dragon lizard Technology
Unity3d click events added to 3D objects in the scene
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
A link opens the applet code. After compilation, it is easy to understand
Unity drawing plug-in = = [support the update of the original atlas]
Simple understanding and application of TS generics
Plate - forme de surveillance par étapes zabbix
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
121. The best time to buy and sell stocks
Continuous creation depends on it!
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
Leetcode-136-只出现一次的数(用异或来解答)
Numpy -- data cleaning
招标公告:盘锦市人民医院盘锦医院数据库维保项目
Vs tool word highlight with margin
Learn good-looking custom scroll bars in 1 minute
Three. JS introductory learning notes 0: illustration of how webgl and threejs work
Mysql database basic operation DQL basic query
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
融云斩获 2022 中国信创数字化办公门户卓越产品奖!