欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP 快速需求路由:FastRoute

程序员文章站 2022-04-20 17:16:43
...
FastRoute 提供了一个快速实现基于路由的规则表达。

示例代码:

addRoute('GET', '/user/{id:\d+}', 'handler1');    $r->addRoute('GET', '/user/{id:\d+}/{name}', 'handler2');    // Or alternatively    $r->addRoute('GET', '/user/{id:\d+}[/{name}]', 'common_handler');});$routeInfo = $dispatcher->dispatch($httpMethod, $uri);switch ($routeInfo[0]) {    case FastRoute\Dispatcher::NOT_FOUND:        // ... 404 Not Found        break;    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:        $allowedMethods = $routeInfo[1];        // ... 405 Method Not Allowed        break;    case FastRoute\Dispatcher::FOUND:        $handler = $routeInfo[1];        $vars = $routeInfo[2];        // ... call $handler with $vars        break;}

项目主页:http://www.open-open.com/lib/view/home/1437228582428