当前位置:网站首页>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
边栏推荐
猜你喜欢
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
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
How does geojson data merge the boundaries of regions?
Description of vs common shortcut keys
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
What about the pointer in neural network C language
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
随机推荐
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Communication mode between application program and MATLAB
How to query the data of a certain day, a certain month, and a certain year in MySQL
95. (cesium chapter) cesium dynamic monomer-3d building (building)
U3D_ Infinite Bessel curve
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
laravel中将session由文件保存改为数据库保存
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
ThinkPHP URL 路由简介
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
Odoo集成Plausible埋码监控平台
PHP实现微信小程序人脸识别刷脸登录功能
Xcode Revoke certificate
Three. JS introductory learning notes 03: perspective projection camera
Asyncio concept and usage
Xcode Revoke certificate
Introduction to pyGame games
Three. JS introductory learning notes 10:three JS grid