建议在Laravel 5中将应用程序数据绑定到请求 [英] Is it advisable to bind application data to request in Laravel 5

查看:77
本文介绍了建议在Laravel 5中将应用程序数据绑定到请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要在中间件中进行身份验证并将某些数据添加到\Illuminate\Http\Request $request对象并通过将\Illuminate\Http\Request $request注入控制器方法中来在控制器中使用该数据,那是否明智?

Would it be advisable, if i am doing authentication in a middleware and adding some data to the \Illuminate\Http\Request $request object and using that data in the controller by injecting \Illuminate\Http\Request $request into the controller method?

原因是,该应用程序需要进行数据库调用以找出凭据是否有效,如果有效,它将返回类似于主键的信息,我将在随后的数据库操作中使用该信息.

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.

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

At the moment, everything is done in the controller. If i were to use a separate middleware for authentication, can i bind the data that my controller needs to the request object if the middleware check passes? if so, how should i go about doing it?

灵感-Expressjs通过一系列中间件/路由沿着请求绑定和传递数据的方式.

Inspiration - Expressjs way of binding and passing data along the request through a stack of middlewares / routes.

推荐答案

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

I dont understand - why dont you just use the Laravel authenticator?

您说:

原因是,该应用程序需要进行数据库调用以找出凭据是否有效,如果有效,它将返回类似于主键的信息,我将在随后的数据库操作中使用该信息.

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 Authenticator所做的吗?

And that is exactly what the Laravel Authenticator does?

然后您就可以在控制器中执行

Then in your controller you can just do

`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软件包.在您的中间件中,您可以执行以下操作:

Meanwhile - you can still use the Laravel Authenticator package whilst using a legacy system for authentication. In your middleware you can do something like this:

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

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

This means you can do your check via the neo4j graph db - and if it returns true that the user is authenticated correctly - then you just log them into the Laravel system yourself.

这篇关于建议在Laravel 5中将应用程序数据绑定到请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆