Home > Backend Development > PHP Tutorial > PHP 快速需求路由:FastRoute

PHP 快速需求路由:FastRoute

WBOY
Release: 2016-06-20 12:52:36
Original
2042 people have browsed it

FastRoute 提供了一个快速实现基于路由的规则表达。

示例代码:

<?phprequire '/path/to/FastRoute/src/bootstrap.php';$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {    $r->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;}
Copy after login

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template