phly/react2psr7

此包已被弃用且不再维护。作者建议使用react/http包。

0.1.1 2016-04-17 17:23 UTC

This package is auto-updated.

Last update: 2021-03-15 21:05:45 UTC


README

Build Status Coverage Status

React中提供PSR-7中间件应用。

安装

$ composer require "react/http:^0.5@dev" phly/react2psr7

react/http

react2psr7目前需要来自即将发布的react/http 0.5版本的特性。由于该版本尚未发布,您需要手动指定它,以强制Composer允许开发版本。

使用方法

以下示例演示了使用React创建HTTP服务器,并使用Expressive应用程序来处理传入的请求。

<?php
use React\EventLoop\Factory;
use React\Http\Server as HttpServer;
use React\Socket\Server as Socket;
use React2Psr7\ReactRequestHandler;
use Zend\Expressive\Application;

require_once 'vendor/autoload.php';

$loop      = Factory::create();
$socket    = new Socket($loop);
$http      = new HttpServer($socket);
$container = require 'config/container.php';

$http->on('request', new ReactRequestHandler($container->get(Application::class)));

// Listen on all ports; omit second argument to restrict to localhost.
$socket->listen(1337, '0.0.0.0');
$loop->run();

提供静态文件

此包还提供了用于提供静态文件的中间件;当作为Web服务器运行React时,这很有用,允许提供CSS、JavaScript和图片。

以下示例展示了如何使用Stratigility构建一个中间件管道,该管道消耗静态文件中间件以及Expressive应用程序,以提供完整的Web服务器。

<?php
use React\EventLoop\Factory;
use React\Http\Server as HttpServer;
use React\Socket\Server as Socket;
use React2Psr7\ReactRequestHandler;
use React2Psr7\StaticFiles;
use Zend\Expressive\Application;
use Zend\Stratigility\MiddlewarePipe;

require_once 'vendor/autoload.php';

$loop      = Factory::create();
$socket    = new Socket($loop);
$http      = new HttpServer($socket);
$container = require 'config/container.php';
$pipeline  = new MiddlewarePipe();

$pipeline->pipe(new StaticFiles());
$pipeline->pipe($container->get(Application::class));

$http->on('request', new ReactRequestHandler($pipeline));

// Listen on all ports; omit second argument to restrict to localhost.
$socket->listen(1337, '0.0.0.0');
$loop->run();

(注意:您还可以将静态文件中间件管道化到Expressive应用程序中。)