简体   繁体   English

将数据绑定到中间件中的请求对象[Slim Framework 3]

[英]Binding data to request object in middleware [Slim Framework 3]

I am decoding jwt tokens in the middleware following this example in the documentation of slim framework. 我将按照苗条框架文档中的示例在中间件中解码jwt令牌。 Now i want to bind userid decoded from the jwt token into the request object. 现在,我要将从jwt令牌解码的userid绑定到请求对象中。 This is how we do it in expressjs but i can't figure out how to do it in slim framework. 这就是我们在expressjs中做到的方式,但我不知道如何在苗条的框架中做到这一点。 Is there anyway to bind data into the request object? 无论如何,有将数据绑定到请求对象中吗?

I tried: 我试过了:

$request->setParam('userid', $userId);

Ok, I have solved this problem. 好的,我已经解决了这个问题。 This is how we can bind data to request object in slim framework. 这就是我们可以在苗条框架中将数据绑定到请求对象的方式。

$request = $request->withAttribute('userid', $userId);

And in the route or controller, this is how we can get data: 在路由或控制器中,这是我们如何获取数据的方式:

$userId = $request->getAttribute('userid');

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

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