当前位置:网站首页>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
边栏推荐
- U3D_ Infinite Bessel curve
- Asyncio concept and usage
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
- 一个普通人除了去工厂上班赚钱,还能干什么工作?
- 强化实时数据管理,英方软件助力医保平台安全建设
- What about the pointer in neural network C language
- Three singleton modes of unity (hungry man, lazy man, monobehavior)
- Enterprise log analysis system elk
- Three. JS introduction learning notes 12: the model moves along any trajectory line
- laravel怎么获取到public路径
猜你喜欢

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

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?

Leetcode-231-2的幂

Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import

Wireless sensor networks -- ZigBee and 6LoWPAN

融云斩获 2022 中国信创数字化办公门户卓越产品奖!

Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages

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

Description of vs common shortcut keys
随机推荐
Notification uses full resolution
神经网络c语言中的指针是怎么回事
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
Shader_ Animation sequence frame
Plate - forme de surveillance par étapes zabbix
121. The best time to buy and sell stocks
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
应用程序和matlab的通信方式
Iptables only allows the specified IP address to access the specified port
Wireless sensor networks -- ZigBee and 6LoWPAN
尤雨溪,来了!
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
Performance measure of classification model
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
php 自带过滤和转义函数
分步式監控平臺zabbix
Three. JS introduction learning notes 12: the model moves along any trajectory line
There are many ways to realize the pause function in JS
MySQL中, 如何查询某一天, 某一月, 某一年的数据
Step by step monitoring platform ZABBIX