tbht/press
Composer 安装命令:
composer require tbht/press
包简介
a frame like nodejs/koa
README 文档
README
This project is a translation of koa framework of nodejs.
Installation
$ composer require tbht/press
Hello Press
use Press\Application; use Press\Context; $app = new Application(); $app->use(function (Context $ctx, callable $next) { $ctx->body = 'Hello Press'; }); $app->listen();
Middleware
Here is an example of logger middleware :
use Press\Context; use Press\Application; $app = new Application(); $app->use(function (Context $ctx, callable $next) { $start = time(); return $next() ->then(function () use($start, $ctx) { $ms = time() - $start; $method = $ctx->method; $url = $ctx->url; print_r("{$method} {$url} - {$ms}ms"); }); });
Document
Application
hello world
use Press\Application; use Press\Context; $app = new Application(); $app->use(function (Context $ctx, callable $next) { $ctx->body = 'Hello World'; }); $app->listen(function () { var_dump('final var dump'); }); // or $app->listen(['port' => 8080]);
Cascading
use Press\Application; use Press\Context; $app = new Application(); // logger $app->use(function (Context $ctx, callable $next) { return $next() ->then(function () use ($ctx) { $rt = $ctx->response->get('x-response-time'); $method = $ctx->method; $url = $ctx->url; echo "{$method} {$url} - {$rt}"; }); }); // x-response-time $app->use(function (Context $ctx, callable $next) { $start = time(); return $next() ->then(function () use ($ctx,$start) { $ms = time() - $start; $ctx->set('x-response-time', "{$ms}ms"); }); }); // response $app->use(function (Context $ctx, callable $next) { $ctx->body = 'Hello World'; });
Settings
-
$app->envdefault to the 'development' -
$app->proxywhen true proxy header fields will be trusted -
$app->subdomainOffsetoffset of.subdomainsto ignore [2]
$app->listen(...)
use Press\Application; $app = new Application(); $app->listen([ "host" => "127.0.0.1", "port" => 8080 ]); // or $app->listen(function () { echo "call back run"; });
$app->callback()
return a callback function suitable for the following http server request.
use React\EventLoop\Factory; $loop = Factory::create(); $server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) { // ... });
$app->use()
see Middlware part
$app->context
$app->context is the prototype from which ctx is created.
$app->use(function ($ctx) { $ctx->db = new DB(); });
Error handling
$app->on("error", function () { echo "this is an error"; });
Context
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2020-07-12