terrydjony/routeria
Composer 安装命令:
composer require terrydjony/routeria
包简介
A simple fast yet powerful PHP router
关键字:
README 文档
README
Routeria is a lightweight and easy-to-use routing component.
Installing
Routeria installation using Composer
composer require terrydjony/routeria ~2.0
Usage
The installed Routeria and all of the components is in the vendor folder.
In order to use it, you just need to require the autoload.
And, you need to load the namespace using use keyword.
require_once __DIR__ . '/vendor/autoload.php';
Configuration (.htaccess)
Before using Routeria, you need to turn your rewrite engine on and add rules so any requests to non-existing directory or filename will be rewritten to index.php.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Simple Callback Routing
For a simple callback route, you just need to use Routeria class which belongs to the Routeria namespace.
The Request component of Symfony HttpFoundation is required to tell the request path to the router.
use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria; $router->get('/', function() { echo 'Hello World';}); $router->route($request->getPathInfo(), $request->getMethod());
Don't forget to write line ->route($request->getPathInfo(), $request->getMethod()); to make it work
Using Named Parameters
use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria; $callback = function($fname, $lname) { echo "Hello $fname $lname. Nice to meet ya!"; }; $router->get('/greet/{fname:alpha}/{lname:alpha}', $callback); $router->route($request->getPathInfo(), $request->getMethod());
The order of parameters in the callback doesn't matter.
You just need to specify all the necessary variables.
There are six placeholders available,
INT for integers (regex: [0-9]+)
ALPHA for alphabets (regex: [a-zA-Z_-]+)
ALNUM for alphanumeric characters (regex: [a-zA-Z0-9_-]+)
HEX for hexadecimals (regex: [0-9A-F]+)
ALL for all characters (regex: .+)
WORD is an alias for ALPHA
Routing with specific HTTP Method
You can also perform other http methods routing easily. (even the custom one)
use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria; $router->get('/', function() { echo 'HTTP METHOD : GET';}); $router->post('/', function() { echo 'HTTP METHOD : POST';}); $router->put('/', function() { echo 'HTTP METHOD : PUT';}); $router->delete('/', function() { echo 'HTTP METHOD : DELETE';}); $router->add('/', function() { echo 'HTTP METHOD : CUSTOM';}, 'CUSTOM'); $router->route($request->getPathInfo(), $request->getMethod());
Different method, different route.
Dispatch Controller
You can also dispatch a controller using Routeria.
use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; class User { public function getInfo($id, $name) { echo 'Hello ' . $name . ' ID: ' . $id; } } $request = Request::createFromGlobals(); $router = new Routeria; $router->get('/user/{name:alpha}/{id:int}', 'User::getInfo'); $router->route($request->getPathInfo(), $request->getMethod());
If you go to '/user/terry/35', the router will dispatch the getInfo method so it prints 'Hello terry ID: 35'.
Don't forget to specify the namespace if the class has.
Converting arguments
use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria; $router->get('/posts/{title:alpha}', function($title) { echo '<h1>'.$title.'</h1>';}) ->convert(function($title) { return ucwords(str_replace('-', ' ', $title)); }); $router->route($request->getPathInfo(), $request->getMethod());
The converter in this example changes all hypens into spaces in the title argument.
So, if you go to '/posts/lorem-ipsum-dolor-sit-amet', it will print <h1>lorem ipsum dolor sit amet</h1>.
Notice that the argument 'lorem-ipsum-dolor-sit-amet' has been converted into 'lorem ipsum dolor sit amet' before the callback fires.
Custom route collection
You can define your own route collection by implementing RouteProviderInterface.
use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; use Routeria\RouteCollection; use Routeria\ControllerRoute; use Routeria\RouteProviderInterface; class BlogCollection implements RouteProviderInterface { public function register(RouteCollection $collection) { $blogRoutes = array( 'index' => new ControllerRoute('/','Blog::index','GET'), 'post' => new ControllerRoute('/{id:int}/{title:alnum}','Blog::showPost','GET'), 'page' => new ControllerRoute('/page/{title:alpha}','Blog::showPage','GET') ); $collection->addRoutes($blogRoutes); } } $request = Request::createFromGlobals(); $router = new Routeria; $collection = new BlogCollection; $router->register($collection); $router->route($request->getPathInfo(), $request->getMethod());
You need your own blog controller to make it work.
Contribute to this library
Please contribute to this project by forking it, make good commits and then perform a pull request.
Thanks for your support.
terrydjony/routeria 适用场景与选型建议
terrydjony/routeria 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「router」 「URL Router」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 terrydjony/routeria 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 terrydjony/routeria 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 terrydjony/routeria 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Write down your routing mapping at one place
Easy URL rewrites in your Laravel application
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
Provides a service and twig extension for getting short urls like http://your.host/~short
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-27