当前位置:网站首页>laravel构造函数和中间件执行顺序问题
laravel构造函数和中间件执行顺序问题
2022-07-07 14:07:00 【全栈程序员站长】
今天想重构下代码结构:
BaseController.php 放置公共的中间件
class BaseController
{
public function __construct(){
$this->middleware('login');
}
}
IndexController.php 继承 BaseController
class IndexController extends BaseController
{
$user; // 存储登录用户实例
public function __construct(){
parent::__constuct();
$this->user = Auth::user();
}
}
--------------------
如上代码,一直报错!提示获取不到用户实例!
各种测试,kernel.php, route.php, BaseController, middleware写法,等等,找不到问题。甚至一度怀疑 parent::__contruct()继承不对...
最终发现,是构造函数和中间件的执行顺序有问题:
1.先执行构造函数
2.再调用中间件
所以,我的问题就是,login中间件要求用户登录,而构造函数又先执行了,直接获取用户信息,还未登录呢!
百度发现一篇文章,描述了这个问题:
https://laravel-china.org/topics/3218/laravel53-middleware-and-controller-construction-method-execution-sequence-problem
/*
它里面提到了一句:
不可在构造函数中,直接获取到session变量或认证后的用户实例!因为中间件还未启动!
这个描述的不准确!
在用户登录后,构造函数中,自然就获取到session变量或认证后的用户实例!
主要还是中间件未在之前执行!说白了就是顺序的问题!
*/
解决方法:
都只能在route.php 中了
1>要么使用 route::get()->middle('login')
2>要么使用 route::get(['middleware' => 'login', xxx])
最好的方法,应该是使用路由组 route::group(['middleware' => 'login'])
或者是:
抛弃在控制器中使用用户实例 发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113214.html原文链接:https://javaforall.cn
边栏推荐
- hellogolang
- [excelexport], Excel to Lua, JSON, XML development tool
- Function: JS Click to copy content function
- Numpy -- epidemic data analysis case
- TCP framework___ Unity
- Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
- Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
- Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
- Logback日志框架第三方jar包 免费获取
- hellogolang
猜你喜欢

Odoo集成Plausible埋码监控平台

Numpy --- basic learning notes

Description of vs common shortcut keys

Numpy -- epidemic data analysis case

Three. JS introductory learning notes 19: how to import FBX static model

Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc

C4D learning notes 1- animation - animation key frames

强化实时数据管理,英方软件助力医保平台安全建设

讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!

numpy--数据清洗
随机推荐
Laravel5.1 路由 -路由分组
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
asyncio 概念和用法
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
Three. JS introductory learning notes 13: animation learning
numpy--疫情数据分析案例
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
一个普通人除了去工厂上班赚钱,还能干什么工作?
Xcode Revoke certificate
Sysom case analysis: where is the missing memory| Dragon lizard Technology
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
Simple understanding and application of TS generics
Three. JS introductory learning notes 18: how to export JSON files with Blender
C4D learning notes 1- animation - animation key frames
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
torch.numel作用
Description of vs common shortcut keys
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)