### Install FastRoute with Composer Source: https://github.com/nikic/fastroute/blob/master/README.md Use this command to install the FastRoute library via Composer. Requires PHP 8.1 or newer. ```sh composer require nikic/fast-route ``` -------------------------------- ### Basic FastRoute Usage Example Source: https://github.com/nikic/fastroute/blob/master/README.md This snippet demonstrates how to set up routes and dispatch an incoming HTTP request using FastRoute. It includes examples of adding routes with different methods, parameters, and optional suffixes, and handling the dispatch results for not found, method not allowed, and found routes. ```php addRoute('GET', '/users', 'get_all_users_handler'); // {id} must be a number (\d+) $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler'); // The /{title} suffix is optional $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler'); }); // Fetch method and URI from somewhere $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; // Strip query string (?foo=bar) and decode URI if (false !== $pos = strpos($uri, '?')) { $uri = substr($uri, 0, $pos); } $uri = rawurldecode($uri); $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; } ``` -------------------------------- ### Shortcut Methods for Common HTTP Methods Source: https://github.com/nikic/fastroute/blob/master/README.md Uses shortcut methods for defining routes for common HTTP request methods like GET and POST. ```php $r->get('/get-route', 'get_handler'); $r->post('/post-route', 'post_handler'); ``` -------------------------------- ### Route Pattern Conversion Example Source: https://github.com/nikic/fastroute/blob/master/README.md Illustrates how a route pattern string like '/user/{id:\d+}[/{name}]' is converted into an array of route information by the RouteParser. This array represents the parsed structure of the route. ```php /* The route /user/{id:\d+}[/{name}] converts to the following array: */ [ [ '/user/', ['id', '\d+'], ], [ '/user/', ['id', '\d+'], '/', ['name', '[^/]+'], ], ] ``` -------------------------------- ### Define Route with Placeholder Matching Any Character Source: https://github.com/nikic/fastroute/blob/master/README.md Defines a route with a placeholder that matches any character until the next slash. ```php $r->addRoute('GET', '/user/{name:.+}', 'handler'); ``` -------------------------------- ### Add a Route Source: https://github.com/nikic/fastroute/blob/master/README.md Adds a route with a specified HTTP method, route pattern, and handler. ```php $r->addRoute($method, $routePattern, $handler); ``` -------------------------------- ### Define Route with Custom Placeholder Regex Source: https://github.com/nikic/fastroute/blob/master/README.md Defines a route with a placeholder that matches a specific regex pattern. ```php $r->addRoute('GET', '/user/{id:\d+}', 'handler'); ``` -------------------------------- ### Add Route with Multiple Methods Source: https://github.com/nikic/fastroute/blob/master/README.md Adds a route that matches multiple HTTP methods using an array. ```php $r->addRoute(['GET', 'POST'], '/test', 'handler'); ``` -------------------------------- ### Overriding FastRoute Components with simpleDispatcher Source: https://github.com/nikic/fastroute/blob/master/README.md Demonstrates how to override the default route parser, data generator, and dispatcher when creating a dispatcher using `simpleDispatcher`. This allows for customization of the routing process. ```php 'FastRoute\RouteParser\Std', 'dataGenerator' => 'FastRoute\DataGenerator\MarkBased', 'dispatcher' => 'FastRoute\Dispatcher\MarkBased', ]); ``` -------------------------------- ### Dispatching a URI with FastRoute Source: https://github.com/nikic/fastroute/blob/master/README.md Call the `dispatch()` method with the HTTP method and URI. The method returns a status code and relevant data, such as allowed methods or route handlers and parameters. ```php /* Routing against GET /user/nikic/42 */ [FastRoute\Dispatcher::FOUND, 'handler0', ['name' => 'nikic', 'id' => '42']] ``` ```php [FastRoute\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'POST']] ``` -------------------------------- ### Cached Dispatcher Configuration Source: https://github.com/nikic/fastroute/blob/master/README.md Configures and creates a cached dispatcher with specified routes and caching options. ```php addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); }, [ 'cacheKey' => __DIR__ . '/route.cache', /* required */ // 'cacheFile' => __DIR__ . '/route.cache', /* will still work for v1 compatibility */ 'cacheDisabled' => IS_DEBUG_ENABLED, /* optional, enabled by default */ 'cacheDriver' => FastRoute\Cache\FileCache::class, /* optional, class name or instance of the cache driver - defaults to file cache */ ]); ``` -------------------------------- ### Define Route with Nested Optional Parts Source: https://github.com/nikic/fastroute/blob/master/README.md Defines a route with multiple nested optional parts at the end of the route pattern. ```php $r->addRoute('GET', '/user[/{id:\d+}[/{name}]]', 'handler'); ``` -------------------------------- ### FastRoute Interfaces for Routing Components Source: https://github.com/nikic/fastroute/blob/master/README.md Defines the interfaces for RouteParser, DataGenerator, and Dispatcher, which are the core components of FastRoute. These interfaces specify the methods required for parsing routes, generating routing data, and dispatching requests. ```php addRoute('GET', '/user/{id:\d+}[/{name}]', 'handler'); ``` -------------------------------- ### Define Routes within a Group Source: https://github.com/nikic/fastroute/blob/master/README.md Defines multiple routes that share a common prefix within a route group. ```php $r->addGroup('/admin', function (FastRoute\ConfigureRoutes $r) { $r->addRoute('GET', '/do-something', 'handler'); $r->addRoute('GET', '/do-another-thing', 'handler'); $r->addRoute('GET', '/do-something-else', 'handler'); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.