austinhyde/patchboard
Composer 安装命令:
composer require austinhyde/patchboard
包简介
A simple router based on nikic/fastroute, providing a higher-level interface and more features
README 文档
README
Building on top of nikic/fastroute, and inspired by the routing portion of the Macaron web framework, this library is a simple, extensible, non-opinionated, and fast routing library for PHP web applications and services.
Important: This is still alpha-quality, but I want to make it real. If you try it out and find any problems, please open an issue!
Install
composer require austinhyde/patchboard
Example
They say an example is worth a thousand words. Or that might be something else.
<?php $req = Request::createFromGlobals(); $authorized = function() use ($req) { list($user, $pass) = explode(':', base64_decode( explode(' ', $req->headers->get('Authorization'))[1] )); if ($user != 'admin' && $pass != 'admin') { return new JSONResponse(['message'=>'Not Authorized'], 403); } }; $json = function($c) use ($req) { $r = $c->nextHandler(); if ($r !== null && !($r instanceof Response)) { return new JSONResponse($r, 200); } }; $r = new Patchboard\Router; $r->group('/users', function($r) { $r->get('/', function() { return [['user_id' => 1, 'username' => 'foo']]; }); $r->get('/{id}', function(Context $ctx) { return ['user_id' => $ctx->getPathParam('id'), 'username' => 'foo']; }); }, $authorized, $json); try { $response = $r->dispatch($req->getMethod(), $req->getPathInfo()); } catch (Patchboard\RouteNotFoundException $ex) { $response = new JSONResponse(['message' => $ex->getMessage()], 404); } catch (Patchboard\MethodNotAllowedException $ex) { $response = new JSONResponse(['message' => $ex->getMessage()], 405, ['Allow' => $ex->getAllowed()]); } $response->send();
Put into words:
- Create a Request object from global variables (from the symfony/http-foundation library)
- Create the "authorized" handler. This is just a function that returns a 403 if the user is not authorized.
- Create the "json" handler. This is a function that converts the result of the next handler in the sequence to JSON.
- Create a new Router object
- Create a group of routes with the
/usersprefix. Note the trailing, $authorized, $json, which instructs the group to apply the$authorizedand$jsonhandler. - Create GET and POST routes for
/users, and instruct the router to call the corresponding controller methods. - Dispatch the call based on HTTP method and path. Handle route-not-found and method-not-allowed errors specially.
- Send the response back to the browser.
Concepts
The driving principle behind Patchboard is to be as simple as possible, while being flexible enough to use in any application. Patchboard does this
by only being a route dispatcher. That's it. At it's core, it's a glorified call_user_func implementation, that pattern matches the function name.
The only assumption it makes is that you are routing on an HTTP method and request path.
The core unit of Patchboard is the route. Each route matches on an HTTP method and path, and has any number of handlers attached to it.
A handler is simply either a callable, or an implementation of the Patchboard\HandlerInterface interface. The callable (or handle method) receives the route context
as an argument. The handler then can do things like authenticate the user, process the HTTP request, transform outputs, etc.
The route context is an object containing information on the matched route and assigned handlers. Handlers can use the hasNextHandler() and handleNext() methods
to see if there's another handler to call, and to invoke the next handler, respectively. handleNext() returns the result of the next handler in the sequence. This
allows you to "wrap" inner handlers and manipulate their results.
For any list of handlers assigned to a route, handlers will be executed sequentially until a non-null result is returned, or there are no more handlers. Handlers that invoke the next handler themselves advance the internal cursor to the next, so that no handlers are called twice.
austinhyde/patchboard 适用场景与选型建议
austinhyde/patchboard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 austinhyde/patchboard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 austinhyde/patchboard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-09