isholao/router
Composer 安装命令:
composer require isholao/router
包简介
PHP request route
README 文档
README
Install
To install with composer:
composer require isholao/router
Requires PHP 7.1 or newer.
Usage
Here's a basic usage example:
<?php require '/path/to/vendor/autoload.php'; $c = \Isholao\Router\RouteCollection(); $c->get('/','get_all_users_responder'); $c->mapMany('GET|POST','/login','login_responder','login'); $c->disptach('GET','/login'); //return Route instance or null
Defining routes
$c = \Isholao\Router\RouteCollection(); $c->mapOne('GET', '/login', 'defined_responder'); $c->mapOne('GET', '/{lang=(?:en|de)}/login', 'defined_responder'); or $c->mapMany('GET|POST', '/login, 'defined_responder');
You can also make an optional path segment by add a ? after the defined segement.
$c = \Isholao\Router\RouteCollection(); $c->mapOne('GET', '/something/{hash=[a-zA-Z]+}?', 'defined_responder'); or $c->mapMany('GET|POST', '/login, 'defined_responder');
You can set a default value for a segment because each defined route returns a \Router\Route instance. Note this can only be done on mapOne method or any of the helper functions get(), post(), head(), delete(), option(), put()
$c = \Isholao\Router\RouteCollection(); $c->mapOne('GET', '/something/{hash=[a-zA-Z]+}', 'defined_responder')->setParam('hash','asd8asdasd9'); or $c->delete('/something/{hash=[a-zA-Z]+}', 'defined_responder')->setParam('hash','asd8asdasd9');
Route Groups
Additionally, you can specify routes inside of a group. All routes defined inside a group will have a common prefix.
For example, defining your routes as:
$c = \Isholao\Router\RouteCollection(); $c->groupRoutes('/admin', function (\Isholao\Router\RouteCollectionInterface $r) { $r->mapOne('GET', '/do-something', 'handler'); // this becomes `/admin/do-something` $r->post('/do-something-else', function(){}); // // this becomes `/admin/do-another-thing` });
Dispatching a URI
A URI is dispatched by calling the dispatch() method of the created \Isholao\Router\RouteCollection object. This method
accepts the HTTP method and a URI.
$c = \Isholao\Router\RouteCollection(); $c->groupRoutes('/admin', function (\Isholao\Router\RouteCollectionInterface $r) { $r->mapOne('GET', '/do-something', 'handler'); // this becomes `/admin/do-something` $r->post('/do-something-else', function(){}); // // this becomes `/admin/do-another-thing` }); $c->dispatch('GET','/admin/do-something'); // return a \Isholao\Router\Route instance or null
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2017-11-21