当前位置:网站首页>Laravel constructor and middleware execution order
Laravel constructor and middleware execution order
2022-07-07 16:19:00 【Full stack programmer webmaster】
Today, I want to refactor the code structure :
BaseController.php Place common middleware
class BaseController
{
public function __construct(){
$this->middleware('login');
}
}
IndexController.php Inherit BaseController
class IndexController extends BaseController
{
$user; // Store login user instances
public function __construct(){
parent::__constuct();
$this->user = Auth::user();
}
}
--------------------
Code above , Has been an error ! Prompt that the user instance cannot be obtained !
Various tests ,kernel.php, route.php, BaseController, middleware How to write it , wait , No problem found . Even doubted for a time parent::__contruct() Wrong inheritance ...
Finally found , There is a problem with the execution order of constructor and middleware :
1. Execute the constructor first
2. Then call the middleware
therefore , My problem is ,login Middleware requires users to log in , And the constructor executes first , Get user information directly , Not logged in yet !
Baidu found an article , Describes the problem :
https://laravel-china.org/topics/3218/laravel53-middleware-and-controller-construction-method-execution-sequence-problem
/*
It mentions a sentence :
Cannot be in constructor , Get... Directly session Variable or authenticated user instance ! Because the middleware has not been started !
This description is not accurate !
After the user logs in , In the constructor , Naturally get session Variable or authenticated user instance !
The main reason is that the middleware was not implemented before ! To put it bluntly, it's a matter of order !
*/
resolvent :
Only in route.php It's in
1> Or use route::get()->middle('login')
2> Or use route::get(['middleware' => 'login', xxx])
The best way , You should use routing groups route::group(['middleware' => 'login'])
Or is it :
Discard the use of user instances in the controller
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/113214.html Link to the original text :https://javaforall.cn
边栏推荐
- nodejs package. JSON version number ^ and~
- 统计学习方法——感知机
- Unity drawing plug-in = = [support the update of the original atlas]
- Particle effect for ugui
- Three. JS introductory learning notes 13: animation learning
- PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
- Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- 分类模型评价标准(performance measure)
- Three singleton modes of unity (hungry man, lazy man, monobehavior)
猜你喜欢
分步式監控平臺zabbix
Wireless sensor networks -- ZigBee and 6LoWPAN
What about the pointer in neural network C language
喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
SPI master RX time out interrupt
Talk about the cloud deployment of local projects created by SAP IRPA studio
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
Xcode Revoke certificate
Power of leetcode-231-2
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
随机推荐
121. 买卖股票的最佳时机
Numpy -- epidemic data analysis case
保证接口数据安全的10种方案
Markdown formula editing tutorial
Dotween -- ease function
Plate - forme de surveillance par étapes zabbix
Migration and reprint
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
Leetcode-231-2的幂
AE learning 01: AE complete project summary
安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
修改配置文件后tidb无法启动
尤雨溪,来了!
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Mysql database basic operation DQL basic query
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Communication mode between application program and MATLAB
js中复选框checkbox如何判定为被选中
Unity drawing plug-in = = [support the update of the original atlas]