当前位置:网站首页>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
边栏推荐
- 用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
- Enterprise log analysis system elk
- 深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
- 分类模型评价标准(performance measure)
- PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
- Xcode Revoke certificate
- Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
- 招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
猜你喜欢
Sysom case analysis: where is the missing memory| Dragon lizard Technology
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
融云斩获 2022 中国信创数字化办公门户卓越产品奖!
神经网络c语言中的指针是怎么回事
Three. JS introductory learning notes 10:three JS grid
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
SPI master RX time out interrupt
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
随机推荐
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
thinkphp3.2.3中设置路由,优化url
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
Leetcode-136-只出现一次的数(用异或来解答)
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
Numpy -- epidemic data analysis case
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
A link opens the applet code. After compilation, it is easy to understand
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
How to query the data of a certain day, a certain month, and a certain year in MySQL
Particle effect for ugui
Vite path alias @ configuration
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
Xcode Revoke certificate
AE learning 01: AE complete project summary
How to determine whether the checkbox in JS is selected
laravel怎么获取到public路径