定制 davidecesarano/embryo-middleware 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

davidecesarano/embryo-middleware

Composer 安装命令:

composer require davidecesarano/embryo-middleware

包简介

A PSR-15 middleware dispatcher.

README 文档

README

Simple dispatcher (PSR-15 server request handler) for a queue of PSR-15 middleware entries. Request handlers and middleware components 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.

Requirements

Installation

Using Composer:

$ composer require davidecesarano/embryo-middleware

Usage

The RequestHandler is a container for a queue of PSR-15 middleware. It takes three methods:

  • the method add add a middleware to the end of the queue.
  • the method prepend add a middleware to the beginning of the queue.
  • the method dispatch requires two arguments, a ServerRequest object and a Response object (used by terminator to return an empty response).

Set ServerRequest and Response

Create ServerRequest and Response objects.

use Embryo\Http\Server\RequestHandler;
use Embryo\Http\Factory\{ServerRequestFactory, ResponseFactory};
use Middlewares\{Uuid, ResponseTime};
use Psr\Http\Message\ServerRequestInterface;

// PSR-7 implementation
$request = (new ServerRequestFactory)->createServerRequestFromServer();
$response = (new ResponseFactory)->createResponse(200);

Add middleware

Create a queue of PSR-15 middleware with the add method or constructor. The add (and prepend) method must be a string or a instance of MiddlewareInterface. In constructor you may create a queue with array of string or instance of MiddlewareInterface.

In this example we use two PSR-15 compatible middleware: Uuid Middleware and ResponseTime Middleware.

// PSR-15 MiddlewareInterface implementation
$middleware = new RequestHandler([
    Uuid::class,
    ResponseTime::class
]);
$response = $middleware->dispatch($request, $response);

Result

The dispatch produces a response messages.

echo 'X-Response-Time: ' . $response->getHeaderLine('X-Response-Time').'<br/>';
echo 'X-Uuid: ' . $response->getHeaderLine('X-Uuid').'<br/>';

echo '<pre>';
    print_r($response->getHeaders());
echo '</pre>';

Example

You may quickly test this using the built-in PHP server going to http://localhost:8000.

$ cd example
$ php -S localhost:8000

统计信息

  • 总下载量: 176
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固