简体   繁体   English

是否可以将应用程序数据绑定到Laravel 5中的请求

[英]Is it advisable to bind application data to request in Laravel 5

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? 如果我在中间件中进行身份验证并将一些数据添加到\\Illuminate\\Http\\Request $request对象并通过将< \\Illuminate\\Http\\Request $request注入控制器方法在控制器中使用该数据,这是否可取?

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? 如果是的话,我应该怎么做呢?

Inspiration - Expressjs way of binding and passing data along the request through a stack of middlewares / routes. 灵感 - Expressjs通过一堆中间件/路由绑定和传递数据的方式。

I dont understand - why dont you just use the Laravel authenticator? 我不明白 - 你为什么不使用Laravel验证器?

You say: 你说:

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. 原因是,应用程序需要进行数据库调用以查明凭据是否有效,如果是,则返回类似我在后续数据库操作中使用的主键。

And that is exactly what the Laravel Authenticator does? 这正是Laravel Authenticator的作用吗?

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.

Edit: Meanwhile - you can still use the Laravel Authenticator package whilst using a legacy system for authentication. 编辑:同时 - 您仍然可以使用Laravel身份验证器包,同时使用旧系统进行身份验证。 In your middleware you can do something like this: 在您的中间件中,您可以执行以下操作:

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

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. 这意味着您可以通过neo4j图形数据库进行检查 - 如果它返回true表示用户已正确认证 - 那么您只需将它们自己登录到Laravel系统即可。

Yes, that is probably a good way to do it, as the build-in Laravel Authentication system works the same way: you can access a logged in user via $request::user() . 是的,这可能是一个很好的方法,因为内置的Laravel身份验证系统的工作方式相同:您可以通过$request::user()访问登录用户。 See http://laravel.com/docs/5.0/authentication#retrieving-the-authenticated-user 请参阅http://laravel.com/docs/5.0/authentication#retrieving-the-authenticated-user

It is ok to validate auth in the middleware. 可以在中间件中验证auth。 In my application we are using the same functionality to check if user sends the right access_code to access to the API methods. 在我的应用程序中,我们使用相同的功能来检查用户是否发送了正确的access_code来访问API方法。 Even Laravel itself handles protected routes by Authenticate middleware. 甚至Laravel本身也通过Authenticate中间件处理受保护的路由。

The problem is, there is no silver bullet of how or where to store additional data. 问题是,没有关于如何或在何处存储其他数据的灵丹妙药。

One method is to store this in the user's session. 一种方法是将其存储在用户的会话中。

The second is to use Illuminate\\Foundation\\Application class itself. 第二种是使用Illuminate\\Foundation\\Application类本身。 You can inject it into your middleware __constructor() and use it to save your data. 您可以将其注入中间件__constructor()并使用它来保存数据。 Application class extends Container class that implements ArrayAccess interface that allows you to access to it's properties like it is an array. Application类扩展了实现ArrayAccess接口的Container类,允许您访问它的属性,就像它是一个数组一样。 That allows you not only to get variables from the Application but to store them too. 这样,您不仅可以从应用程序中获取变量,还可以存储它们。 Not the best way though the simplest. 虽然最简单,但不是最好的方法。

public function __construct(\Illuminate\Foundation\Application $app)
{
    $app['_foo'] = 'bar';
}

There are more such hacks but these are the simplest. 还有更多这样的黑客,但这些是最简单的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM