POA

POA(Php cOroutine based Application framework)Web框架,灵感来自于 Node.js 的 KOA 框架,基于 React-PHP, 使用 PHP 的生成器来实现中间件,轻松的组合管理多个中间件之间的协作。

作者

York

安装

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.

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。