andrewcarteruk/simple-route
Composer 安装命令:
composer require andrewcarteruk/simple-route
包简介
An easy to use wrapper for the FastRoute package
README 文档
README
This easy to use router is a simple wrapper for the FastRoute library.
Install
Install using Composer.
composer require andrewcarteruk/simple-route ^0.2
Example Usage
use SimpleRoute\Route; use SimpleRoute\Router; $router = Router::fromArray([ new Route('GET', '/', 'handler1'), new Route('GET', '/{page}', 'handler2'), ]); try { $result = $router->match($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); $handler = $result->getHandler(); $params = $result->getParams(); // ... } catch (SimpleRoute\Exception\NotFoundException $exception) { // ... } catch (SimpleRoute\Exception\MethodNotAllowedException $exception) { // ... }
Documentation
The following documentation is derived from the FastRoute documentation.
Routes are defined as an array of SimpleRoute\Route objects that are passed to
SimpleRoute\Router::fromArray(array $routes).
SimpleRoute\Route objects require a $method, a $pattern and a $handler:
$route = new Route($method, $pattern, $handler);
The $method is an uppercase HTTP method string for which a certain route
should match. It is possible to specify multiple valid methods using an array:
$routes = [ // This route new Route(['GET', 'POST'], '/test', 'handler'), // Is equivalent to these two routes together new Route('GET', '/test', 'handler'), new Route('POST', '/test', 'handler'), ];
By default the $pattern uses a syntax where {foo} specifies a placeholder
with name foo and matching the regex [^/]+. To adjust the pattern the
placeholder matches, you can specify a custom pattern by writing {bar:[0-9]+}.
Some examples:
$routes = [ // Matches /user/42, but not /user/xyz new Route('GET', '/user/{id:\d+}', 'handler'), // Matches /user/foobar, but not /user/foo/bar new Route('GET', '/user/{name}', 'handler'), // Matches /user/foo/bar as well new Route('GET', '/user/{name:.+}', 'handler'), ];
Custom patterns for route placeholders cannot use capturing groups. For example
{lang:(en|de)} is not a valid placeholder, because () is a capturing group.
Instead you can use either {lang:en|de} or {lang:(?:en|de)}.
Furthermore parts of the route enclosed in [...] are considered optional, so
that /foo[bar] will match both /foo and /foobar. Optional parts are only
supported in a trailing position, not in the middle of a route.
$routes = [ // This route new Route('GET', '/user/{id:\d+}[/{name}]', 'handler'), // Is equivalent to these two routes together new Route('GET', '/user/{id:\d+}', 'handler'), new Route('GET', '/user/{id:\d+}/{name}', 'handler'), // This route is NOT valid, because optional parts can only occur at the end new Route('GET', '/user[/{id:\d+}]/{name}', 'handler'), ];
The $handler parameter does not necessarily have to be a callback, it could
also be a controller class name or any other kind of data you wish to associate
with the route. SimpleRoute only tells you which handler corresponds to your
URI, how you interpret it is up to you.
Credits
This library is merely a wrapper for FastRoute that aims to provide an easier to use API.
The author of FastRoute is Nikita Popov.
andrewcarteruk/simple-route 适用场景与选型建议
andrewcarteruk/simple-route 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 215 次下载、GitHub Stars 达 16, 最近一次更新时间为 2016 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「routing」 「router」 「Simple」 「fast」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andrewcarteruk/simple-route 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andrewcarteruk/simple-route 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andrewcarteruk/simple-route 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
A Laravel-like router for the WordPress Rewrite API
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
A Laravel helper to detect if the current route/path is active.
Router
统计信息
- 总下载量: 215
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-25