php - 在 Laravel 5 中将应用程序数据绑定(bind)到请求是否可取

标签 php laravel middleware

如果我在中间件中进行身份验证并将一些数据添加到 \Illuminate\Http\Request $request 对象并通过注入(inject) 在 Controller 中使用该数据是否可取\Illuminate\Http\Request $request 进入 Controller 方法?

原因是,该应用程序需要进行数据库调用以查明凭据是否有效,如果有效,它会返回类似于我在后续数据库操作中使用的主键的内容。

目前,一切都在 Controller 中完成。如果我要使用单独的中间件进行身份验证,如果中间件检查通过,我可以将我的 Controller 需要的数据绑定(bind)到请求对象吗?如果是这样,我应该怎么做?

灵感 - Expressjs 通过中间件/路由堆栈绑定(bind)和传递数据的方式。

最佳答案

我不明白 - 为什么不直接使用 Laravel 身份验证器?

你说:

The reason, the app needs to make a database call to find out if the credentials are valid and if it is, it returns something like a primary key that i use in subsequent db operations.

这正是 Laravel 身份验证器所做的?

然后在你的 Controller 中你可以做

`auth()->user()` // gives you the user record
`auth()->id()` // user_id from the DB
`auth()->user()->name` // gives you the `name` column off the record. You can change it to anything.

编辑:同时 - 您仍然可以在使用遗留系统进行身份验证的同时使用 Laravel Authenticator 包。在您的中间件中,您可以执行以下操作:

if (doLegacyCheckHere()) {
      Auth::loginUsingId(1);
}

这意味着您可以通过 neo4j 图形数据库进行检查 - 如果它返回 true 表明用户已正确验证 - 那么您只需自己将他们登录到 Laravel 系统即可。

关于php - 在 Laravel 5 中将应用程序数据绑定(bind)到请求是否可取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29604961/

相关文章:

php - 解析MySQL准备语句

php - 将 2 个 Select 语句与不同的 Where 子句连接在一起并在 While 循环中使用

laravel - 如何设置 Laravel 每天的速率限制器?

php - 中间件中的 Laravel 依赖注入(inject)

javascript - 使用 javascript 更改 iframe 中的链接

php - mysqli_result 类实际获取的数据存在于何处?

sql - 在laravel中链接2个数据集的最有效方法

laravel - 每次加载页面时在 Laravel 中运行命令

java - 如何导入中间件库

asp.net-core - ServiceStack 和 .NET Core 中间件