翻译进度
5
分块数量
2
参与人数

PSR-15 HTTP 请求处理器

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。

HTTP Server Request Handlers

This document describes common interfaces for HTTP server request handlers ("request handlers") and HTTP server middleware components ("middleware") that use HTTP messages as described by PSR-7 or subsequent replacement PSRs.

HTTP request handlers are a fundamental part of any web application. Server side code receives a request message, processes it, and produces a response message. HTTP middleware is a way to move common request and response processing away from the application layer.

The interfaces described in this document are abstractions for request handlers and middleware.

Note: All references to "request handlers" and "middleware" are specific to server request processing.

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

References

1. Specification

1.1 Request Handlers

A request handler is an individual component that processes a request and produces a response, as defined by PSR-7.

A request handler MAY throw an exception if request conditions prevent it from producing a response. The type of exception is not defined.

Request handlers using this standard MUST implement the following interface:

  • Psr\Http\Server\RequestHandlerInterface

1.2 Middleware

A middleware component is an individual component participating, often together with other middleware components, in the processing of an incoming request and the creation of a resulting response, as defined by PSR-7.

A middleware component MAY create and return a response without delegating to a request handler, if sufficient conditions are met.

Middleware using this standard MUST implement the following interface:

  • Psr\Http\Server\MiddlewareInterface

1.3 Generating Responses

It is RECOMMENDED that any middleware or request handler that generates a response will either compose a prototype of a PSR-7 ResponseInterface or a factory capable of generating a ResponseInterface instance in order to prevent dependence on a specific HTTP message implementation.

1.4 Handling Exceptions

It is RECOMMENDED that any application using middleware include a component that catches exceptions and converts them into responses. This middleware SHOULD be the first component executed and wrap all further processing to ensure that a response is always generated.

2. 接口

2.1 Psr\Http\Server\RequestHandlerInterface

下面的接口必须由请求处理程序实现。

namespace Psr\Http\Server;

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

/**
 * 处理服务器请求并返回响应
 *
 * HTTP 请求处理程序处理 HTTP 请求,以便生成 HTTP 相应。
 */
interface RequestHandlerInterface
{
    /**
     * 处理服务器请求并返回响应
     *
     * 可以调用其他协助代码来生成响应。
     */
    public function handle(ServerRequestInterface $request): ResponseInterface;
}
Everan 翻译于 1个月前

2.2 Psr\Http\Server\MiddlewareInterface

The following interface MUST be implemented by compatible middleware components.

namespace Psr\Http\Server;

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

/**
 * Participant in processing a server request and response
 *
 * An HTTP middleware component participates in processing an HTTP message:
 * by acting on the request, generating the response, or forwarding the
 * request to a subsequent middleware and possibly acting on its response.
 */
interface MiddlewareInterface
{
    /**
     * Process an incoming server request
     *
     * Processes an incoming server request in order to produce a response.
     * If unable to produce the response itself, it may delegate to the provided
     * request handler to do so.
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface;
}

本文章首发在 Laravel China 社区
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

参与译者:2
讨论数量: 0
发起讨论


暂无话题~