当前位置:网站首页>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
边栏推荐
- torch. Numel action
- Particle effect for ugui
- laravel post提交数据时显示异常
- Unity3d click events added to 3D objects in the scene
- PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
- php 自带过滤和转义函数
- 2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
- U3D_ Infinite Bessel curve
猜你喜欢

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

Rongyun won the 2022 China Xinchuang digital office portal excellence product award!

山东老博会,2022中国智慧养老展会,智能化养老、适老科技展

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo

企业级日志分析系统ELK

MySQL数据库基本操作-DQL-基本查询

模仿企业微信会议室选择

C4D learning notes 2- animation - timeline and time function

Three. JS introductory learning notes 18: how to export JSON files with Blender

【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
随机推荐
What else can an ordinary person do besides working in a factory to make money?
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
无线传感器网络--ZigBee和6LoWPAN
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
The unity vector rotates at a point
强化实时数据管理,英方软件助力医保平台安全建设
Three. JS introductory learning notes 03: perspective projection camera
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
目标跟踪常见训练数据集格式
Odoo集成Plausible埋码监控平台
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
iptables只允许指定ip地址访问指定端口
Sysom case analysis: where is the missing memory| Dragon lizard Technology
Unity drawing plug-in = = [support the update of the original atlas]
laravel构造函数和中间件执行顺序问题
Shader basic UV operations, translation, rotation, scaling
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition