当前位置:网站首页>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
边栏推荐
- 统计学习方法——感知机
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
- The unity vector rotates at a point
- TCP framework___ Unity
- Application example of infinite list [uigridview]
- laravel post提交数据时显示异常
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- Regular expression string
- C4D learning notes 1- animation - animation key frames
- 121. The best time to buy and sell stocks
猜你喜欢
Plate - forme de surveillance par étapes zabbix
JS array foreach source code parsing
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
C4D learning notes 3- animation - animation rendering process case
Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
Three. JS introductory learning notes 18: how to export JSON files with Blender
随机推荐
laravel怎么获取到public路径
How to determine whether the checkbox in JS is selected
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
Mysql database basic operation DQL basic query
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Leetcode-136- number that appears only once (solve with XOR)
Statistical learning method -- perceptron
Asyncio concept and usage
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
Three. JS introductory learning notes 15: threejs frame animation module
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
laravel post提交数据时显示异常
iptables只允许指定ip地址访问指定端口
招标公告:盘锦市人民医院盘锦医院数据库维保项目
js中复选框checkbox如何判定为被选中
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
统计学习方法——感知机
TCP framework___ Unity
Regular expression string