PSR-7的“属性"在响应对象上 [英] PSR-7 "attributes" on Response object

查看:92
本文介绍了PSR-7的“属性"在响应对象上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PSR-7(带有Zend Expressive)进行开发.我想出了方法

I'm developing using PSR-7 (with Zend Expressive). I figured out the method

ServerRequestInterface::withAttribute()

,我想知道为什么对象Response没有一个. 我想在处理后,在响应端"将元数据通过中间件传递. 是否可以通过某种方式在Response上传递属性"以进行后处理?遵循体系结构指南,实现此目标的最佳方法是什么?

and I was wondering why the object Response doesn't have one. I'd like to pass metadata through middlewares after processing, on "response side". Is there somehow to pass "attributes" on Response for post-processing? What's is the best way, following the architecture guidelines, to achieve that?

推荐答案

最佳实践是使用请求对象在中间件之间传递数据.响应就是要发送给客户端的内容,您希望保持这种状态.该请求仅存在于服务器上,您可以添加(敏感数据)属性以进行传递.如果出现问题或您在删除自定义数据之前提早返回了响应,则没关系,因为您的响应是干净的".

Best practise is using the request object to pass data between Middleware. The response is what is going out to the client and you want to keep this clean. The request lives only on the server and you can add (sensitive data) attributes to pass around. In case something goes wrong or you return a response early before removing the custom data, then it doesn't matter since your response is "clean".

此外,如果您需要传递数据:中间件总是按照从配置中获取的顺序执行.这样,您可以确保MiddlewareX中的请求对象包含MiddlewareY设置的数据.

Also if you need to pass data around: The Middleware is always executed in the order it gets from the config. This way you can make sure the request object in MiddlewareX contains the data set by MiddlewareY.

更新:有关如何通过请求传递数据的示例.

UPDATE: An example on how to pass data with the a request.

中间件2设置一个messanger对象,中间件4可以使用该对象来设置再次出路所需的数据.

Middleware 2 sets an messanger object which Middleware 4 can use to set data which is required on the way out again.

<?php

namespace Middleware;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class Middleware2
{
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        $messenger = new Messenger();

        // Do something else before next middleware

        if ($next) {
            $response = $next($request->withAttribute(Messenger::class, $messenger), $response);
        }  

        // Do something with the Response after it got back
        // At this point the $messenger object contains the updated data from Middleware4

        return $response->withHeader('Content-Language', $locale);
    }
}

中间件4捕获Messenger对象并更新其值.

Middleware 4 grabs the messenger object and updates its values.

<?php

namespace Middleware;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class Middleware4
{
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        $messenger = $request->getAttribute(Messenger::class);
        $messenger->info('going in');
        // Do something else before next middleware

        if ($next) {
            $response = $next($request->withAttribute(FlashMessenger::class, $messenger), $response);
        }  

        // Do something with the Response after it got back
        $messenger->info('going out');

        return $response->withHeader('Content-Language', $locale);
    }
}

这篇关于PSR-7的“属性"在响应对象上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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