mbarquin/reactphp-slim
Composer 安装命令:
composer require mbarquin/reactphp-slim
包简介
Slim Request and Response extended to allow reactPHP serve a Slim framework instance
README 文档
README
Introduction
This library is created in order to use reactPHP as a HTTP server for Slim framework. It will launch a Slim\App process when a request is made, and at the same time it will transfer data from reactPHP native objects into Slim objects. With this, we will be able to create a basic react server for a Slim framework application.
Data, cookies and file uploads transmission between react and Slim objects. You can access through slim native functions to uploaded files, data and cookies.
##Installation You can install the component in the following ways:
- Use the official Github repository (https://github.com/mbarquin/reactphp-slim.git)
- Use composer : composer require mbarquin/reactphp-slim --dev
##Usage
After the composer autoload requirement a Slim\App should be instanced and prepared as usual. Slim\App can be bootstrapped and all dependencies can be injected as you like, after that, a reactphp-slim server should be instanced and call the run method in it, using slim\App as parameter. The reactphp-slim server will act as intermediary and will launch the slim application through the process method when requested, this method avoids the usual request and response bootstrap made by Slim.
When uploading files, move_uploaded_files() probably won't work, use native object methods to move the file.
require '../vendor/autoload.php'; use \mbarquin\reactSlim; // We keep a new Slim app instance. $app = new \Slim\App(); // We add a closure to attend defined request routes $app->any('/hello/{name}', function ( \Slim\Http\Request $request, \Slim\Http\Response $response) { $name = $request->getAttribute('name'); $response->getBody()->write("Hello, $name"); $response->getBody()->write(print_r($request->getParsedBody(), true)); $response->getBody()->write(print_r($request->getCookieParams(), true)); $response->getBody()->write(print_r($request->getHeaders(), true)); // $response->getBody()->write(print_r($request->getUploadedFiles(), true)); return $response; }); $server = new \mbarquin\reactSlim\Server(); $server->withHost('192.168.67.1')->withPort(1337)->run($app);
\mbarquin\reactSlim\Server object is the class which is going to configure and launch a ReactPHP server. It has two main methods
withHost($string) Sets the IP to be listened to
withPort($int) Sets the port the server will be listening to, by default it will be set to 1337.
run(\Slim\App $app) It launches the server process and wait until a request is made to launch the \Slim\App passed as parameter.
v0.4.2 Setup
This is the old setup to run the reactPHP server with a slimPHP application
require '../vendor/autoload.php'; use mbarquin\reactSlim; // We keep a new Slim app instance. $app = new \Slim\App(); // We add a closure to listen defined request routes $app->get('/hello/{name}', function ( \mbarquin\reactSlim\Request $request, \mbarquin\reactSlim\Response $response) { $name = $request->getAttribute('name'); $response->getBody()->write("Hello, $name"); return $response; }); // We create a closure to be attached to server request event. $serverCallback = function ( \React\Http\Request $request, \React\Http\Response $response) use ($app){ $slRequest = \mbarquin\reactSlim\Request::createFromReactRequest($request); $slResponse = new \mbarquin\reactSlim\Response(); $app->process($slRequest, $slResponse); $slResponse->setReactResponse($response, true); }; // We make the setup of the ReactPHP $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new React\Http\Server($socket, $loop); // Ligamos la closure al evento request. $http->on('request', $serverCallback); echo "Server running at http://127.0.0.1:1337\n"; $socket->listen(1337); $loop->run();
mbarquin/reactphp-slim 适用场景与选型建议
mbarquin/reactphp-slim 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 247 次下载、GitHub Stars 达 22, 最近一次更新时间为 2016 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mbarquin/reactphp-slim 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mbarquin/reactphp-slim 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 247
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-13