vakata / router
简单的请求路由器
3.1.3
2016-10-28 10:08 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- clean/phpdoc-md: dev-master
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: 4.*
README
简单的请求路由器。
安装
通过 Composer
$ composer require vakata/router
用法
// create an instance $router = new \vakata\router\Router(); $router ->get('/', function () { echo 'homepage'; }) ->get('/profile', function () { echo 'user profile'; }) ->group('/books/', function ($router) { // specify a prefix $router ->get('read/{i:id}', function ($matches) { // this method uses a named placeholder // when visiting /books/read/10 matches will contain: var_dump($matches); // 0 => books, 1 => read, 2 => 10, id => 10 // placeholders are wrapped in curly braces {...} and can be: // - i - an integer // - a - any letter (a-z) // - h - any letter or integer // - * - anything (up to the next slash (/)) // - ** - anything (to the end of the URL) // placeholders can be named too by using the syntax: // {placeholder:name} // placeholders can also be optional // {?optional} }) // for advanced users - you can use any regex as a placeholder: ->get('{(delete|update):action}/{(\d+):id}', function ($matches) { }) // you can also use any HTTP verb ->post('delete/{i:id}', function ($matches) { }) }) // you can also bind multiple HTTP verbs in one go ->add(['GET', 'HEAD'], '/path', function () { }) // you can also use with() statements to execute some code if the begging of the URL is a match to the prefix ->with('user', function () { echo 1; }) ->get('view', function () { /* 1 will be echoed */ }) ->post('chat', function () { /* 1 will be echoed */ }); // there is no need to chain the method calls - this works too: $router->post('123', function () { }); $router->post('456', function () { }); // you finally run the router try { $router->run( parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), $_SERVER['REQUEST_METHOD'] ); } catch (\vakata\router\RouterNotFoundException $e) { // thrown if no matching route is found }
在 API 文档 中阅读更多
测试
$ composer test
贡献
请参阅 贡献指南 了解详情。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 github@vakata.com 反馈,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。