york8 / poa
POA Web框架灵感来自于Node.js的KOA框架,基于React-PHP,使用生成器来实现中间件,轻松的组合管理多个中间件之间的协作
0.4.1
2018-02-28 10:38 UTC
Requires
- php: >=7.1.0
- psr/http-message: ~1.0
- psr/log: ^1.0
- react/event-loop: ^0.4.3
- react/http: ^0.7.4
- york8/router: ~0.1.1
This package is not auto-updated.
Last update: 2024-11-10 05:41:38 UTC
README
POA(Php cOroutine based Application framework)Web框架,灵感来自于 Node.js 的 KOA 框架,基于 React-PHP, 使用 PHP 的生成器来实现中间件,轻松的组合管理多个中间件之间的协作。
作者
安装
composer require york8/poa
使用
// 1. create the Application $app = new Application(); // 2. define the route rules $router = new RouterMiddleware(function (Context $context) { $context->statusCode(404)->send('Not Found'); }); $router->get( '/foo/bar$', function (Context $context) { $context->send('Hello, ' . $context->getRequest()->getUri()); } )->get( '/foo/exception', function () { throw new Exception('I throw an exception just for fun, haha!'); } ); // 3. use middlewares what you need $app->use(new ProfileMiddleware()) // simple error handle middleware ->use(function (Context $context) { try { yield; } catch (Exception $e) { fwrite(STDERR, 'EXP: ' . $e->getMessage() . "\n"); $context->statusCode(500)->send('Exp: ' . $e->getMessage()); } }) ->use(function (Context $context) { $uri = $context->getRequest()->getUri(); if ($uri->getPath() === '/bar') { $context->send('you fire.'); return false; } return null; }) ->use($router) // global error handle middleware ->useErrorMiddleware(function (Throwable $throwable, Context $context) { // handle global exception $msg = $throwable->getMessage(); fwrite(STDERR, 'Global Exp: ' . $msg . "\n"); if ($context) { $context->statusCode(500)->send('Oh, No! ' . $msg); } }); // 4. listen and start the server $app->listen(8088);
License
The MIT License (MIT). Please see License File for more information.