当前位置:网站首页>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
边栏推荐
- Odoo集成Plausible埋码监控平台
- What about the pointer in neural network C language
- 讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
- JS array foreach source code parsing
- Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
- Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
- Unity drawing plug-in = = [support the update of the original atlas]
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- 强化实时数据管理,英方软件助力医保平台安全建设
- C4D learning notes 2- animation - timeline and time function
猜你喜欢

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

95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)

Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)

Numpy -- epidemic data analysis case

航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆

Sysom case analysis: where is the missing memory| Dragon lizard Technology

10 schemes to ensure interface data security

C4D learning notes 2- animation - timeline and time function

AE learning 02: timeline

AE learning 01: AE complete project summary
随机推荐
There are many ways to realize the pause function in JS
What else can an ordinary person do besides working in a factory to make money?
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
Sysom case analysis: where is the missing memory| Dragon lizard Technology
A JS script can be directly put into the browser to perform operations
模仿企业微信会议室选择
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
How to query the data of a certain day, a certain month, and a certain year in MySQL
Application example of infinite list [uigridview]
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
Xcode Revoke certificate
three.js打造酷炫下雪效果
[excelexport], Excel to Lua, JSON, XML development tool
通知Notification使用全解析
Power of leetcode-231-2
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Numpy --- basic learning notes
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
修改配置文件后tidb无法启动
Three. JS introductory learning notes 10:three JS grid