krak / http
用于构建框架的 Http 工具
v0.3.3
2017-05-04 04:50 UTC
Requires
- nikic/iter: ^1.4
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.3
- krak/cargo: ^0.2.4
- krak/mw: ^0.5.0
- krak/php-inc: ^0.1.3
- nikic/fast-route: ^1.0
- peridot-php/peridot: ^1.18
- symfony/var-dumper: ^3.2
- zendframework/zend-diactoros: ^1.3
Suggests
- nikic/fast-route: For the FastRoute dispatcher
README
Krak Http 包是一组构建 Http 应用程序的实用工具。它包含一个与实现无关的路由系统、PSR-7 响应工厂、PSR-7 服务器实现以及一些有用的中间件。
安装
通过 composer 在 krak/http
下安装
使用方法
响应工厂
<?php interface ResponseFactory { public function createResponse($status = 200, array $headers = [], $body = null); }
每个响应工厂都必须实现该接口。
<?php use Krak\Http\ResponseFactory; $rf = new ResponseFactory\DiactorosResponseFactory(); $rf = new ResponseFactory\GuzzleResponseFactory(); // adds html content-type header $html_rf = new ResponseFactory\HtmlResponseFactory($rf); // json encodes the body and add json content-type header. Accepts json_encode_options as second parameter $json_rf = new ResponseFactory\JsonResponseFactory($rf, JSON_PRETTY_PRINT); // adds text content-type header $text_rf = new ResponseFactory\TextResponseFactory($rf); $json_rf->createResponse(200, [], [1,2,3]);
路由
<?php use Krak\Http\Route; $routes = new Route\RouteGroup(); $routes->get('/', function() {})->with('attribute', 'value'); $routes->group('/foo', function($foo) { $sub->get('', 'handler'); $sub->group('/bar', function($bar) { $bar->get('/baz', 'handler'); }); }); $routes->with('attribute1', 'value');
编译路由
一旦创建了一组路由,可以使用路由编译器来编译它们。这些路由将遍历路由层次结构并将其扁平化为具有标准化路径的迭代器。
<?php use Krak\Http\Route; $routes = new Route\RouteGroup(); // add routes to $routes $compiler = new Route\RecursiveRouteCompiler(); // compile on a path $routes = $compiler->compileRoutes($routes, '/');
分发
要分发一组路由,需要创建分发器工厂,该工厂将从一组路由创建分发器,然后可以分发一个 PSR-7 请求。
<?php use Krak\Http\Dispatcher; $dispatch_factory = new Dispatcher\FastRoute\FastRouteDispatcherFactory(); $dispatch = $dispatch_factory->createDispatcher($routes); $res = $dispatch->dispatch($req); // $res->status_code // $res->matched_route->route // $res->matched_route->params // $res->allowed_methods /* if status code is a 405 response */
服务器
服务器负责创建请求并发出响应。这是一个简单的接口
<?php interface Server { /** @param $handler resolves the request into a response object */ public function serve($handler); }
<?php $server = new Krak\Http\Server\DiactorosServer(); $server->serve(function($req) { return new Zend\Diactoros\Response(); });
中间件
以下是一些在您的应用程序中使用的有用中间件。每个中间件接受两个参数:PSR-7 服务器请求和 HttpLink。如果您想了解更多关于 Link 的工作方式的信息,请查看 Krak\Mw 库。
<?php use Psr\Http\Message\ServerRequestInterface; use Krak\Http\Middleware\HttpLink; function myMiddleware() { return function(ServerRequestInterface $req, HttpLink $next) { if (certainCondition($req)) { return $next($req->withAttribute('a', '1'))->withStatusCode(404); } return $next->response(404, ['X-Header' => 'value'], 'Some body'); // can also use a php or psr-7 stream. }; }
injectRequestAttribute($name, $value)
这将自动注入一个具有指定名称和值的属性。
wrap($psr7_middleware)
这将包装使用请求和响应作为中间件参数的 PSR-7 样式中间件。
<?php $mw = Krak\Http\Middleware\wrap(function($req, $resp, callable $next) { });
serveStatic($root)
这将静置并检查 URI 路径是否存在文件。如果存在,则将文件服务,否则将传递给下一个中间件。
mount($path, $mw)
在路径前缀上挂载中间件。如果匹配路径前缀,则调用中间件。
<?php use function Krak\Http\Middleware\{mount, serveStatic}; $mw = mount('/assets', serveStatic(__DIR__ . '/path/to/assets'));
上述中间件将尝试在 /assets
uri 上加载文件。因此,GET /assets/app.css
将返回 CSS 文件内容 如果 __DIR__ . /path/to/assets/app.css
存在于文件系统中。
测试和示例
通过
make test