[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-8343":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":9,"language":10,"languages":9,"totalLinesOfCode":9,"stars":11,"forks":12,"watchers":13,"openIssues":14,"contributorsCount":15,"subscribersCount":15,"size":15,"stars1d":15,"stars7d":15,"stars30d":16,"stars90d":15,"forks30d":15,"starsTrendScore":15,"compositeScore":17,"rankGlobal":9,"rankLanguage":9,"license":18,"archived":19,"fork":19,"defaultBranch":20,"hasWiki":21,"hasPages":19,"topics":22,"createdAt":9,"pushedAt":9,"updatedAt":23,"readmeContent":24,"aiSummary":25,"trendingCount":15,"starSnapshotCount":15,"syncStatus":26,"lastSyncTime":27,"discoverSource":28},8343,"FastRoute","nikic\u002FFastRoute","nikic","Fast request router for PHP",null,"PHP",5269,453,175,18,0,6,64.57,"Other",false,"master",true,[],"2026-06-12 04:00:38","FastRoute - Fast request router for PHP\n=======================================\n\n[![Build Status](https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Factions\u002Fworkflow\u002Fstatus\u002Fnikic\u002FFastRoute\u002Fphpunit.yml?branch=master&style=flat-square)](https:\u002F\u002Fgithub.com\u002Fnikic\u002FFastRoute\u002Factions?query=workflow%3A%22PHPUnit%20Tests%22+branch%3Amaster)\n\nThis library provides a fast implementation of a regular expression based router. [Blog post explaining how the\nimplementation works and why it is fast.][blog_post]\n\nInstall\n-------\n\nTo install with composer:\n\n```sh\ncomposer require nikic\u002Ffast-route\n```\n\nRequires PHP 8.1 or newer.\n\nUsage\n-----\n\nHere's a basic usage example:\n\n```php\n\u003C?php\n\nrequire '\u002Fpath\u002Fto\u002Fvendor\u002Fautoload.php';\n\n$dispatcher = FastRoute\\simpleDispatcher(function(FastRoute\\ConfigureRoutes $r) {\n    $r->addRoute('GET', '\u002Fusers', 'get_all_users_handler');\n    \u002F\u002F {id} must be a number (\\d+)\n    $r->addRoute('GET', '\u002Fuser\u002F{id:\\d+}', 'get_user_handler');\n    \u002F\u002F The \u002F{title} suffix is optional\n    $r->addRoute('GET', '\u002Farticles\u002F{id:\\d+}[\u002F{title}]', 'get_article_handler');\n});\n\n\u002F\u002F Fetch method and URI from somewhere\n$httpMethod = $_SERVER['REQUEST_METHOD'];\n$uri = $_SERVER['REQUEST_URI'];\n\n\u002F\u002F Strip query string (?foo=bar) and decode URI\nif (false !== $pos = strpos($uri, '?')) {\n    $uri = substr($uri, 0, $pos);\n}\n$uri = rawurldecode($uri);\n\n$routeInfo = $dispatcher->dispatch($httpMethod, $uri);\nswitch ($routeInfo[0]) {\n    case FastRoute\\Dispatcher::NOT_FOUND:\n        \u002F\u002F ... 404 Not Found\n        break;\n    case FastRoute\\Dispatcher::METHOD_NOT_ALLOWED:\n        $allowedMethods = $routeInfo[1];\n        \u002F\u002F ... 405 Method Not Allowed\n        break;\n    case FastRoute\\Dispatcher::FOUND:\n        $handler = $routeInfo[1];\n        $vars = $routeInfo[2];\n        \u002F\u002F ... call $handler with $vars\n        break;\n}\n```\n\n### Defining routes\n\nThe routes are defined by calling the `FastRoute\\simpleDispatcher()` function, which accepts\na callable taking a `FastRoute\\ConfigureRoutes` instance. The routes are added by calling\n`addRoute()` on the collector instance:\n\n```php\n$r->addRoute($method, $routePattern, $handler);\n```\n\nThe `$method` is an uppercase HTTP method string for which a certain route should match. It\nis possible to specify multiple valid methods using an array:\n\n```php\n\u002F\u002F These two calls\n$r->addRoute('GET', '\u002Ftest', 'handler');\n$r->addRoute('POST', '\u002Ftest', 'handler');\n\u002F\u002F Are equivalent to this one call\n$r->addRoute(['GET', 'POST'], '\u002Ftest', 'handler');\n```\n\nBy default, the `$routePattern` uses a syntax where `{foo}` specifies a placeholder with name `foo`\nand matching the regex `[^\u002F]+`. To adjust the pattern the placeholder matches, you can specify\na custom pattern by writing `{bar:[0-9]+}`. Some examples:\n\n```php\n\u002F\u002F Matches \u002Fuser\u002F42, but not \u002Fuser\u002Fxyz\n$r->addRoute('GET', '\u002Fuser\u002F{id:\\d+}', 'handler');\n\n\u002F\u002F Matches \u002Fuser\u002Ffoobar, but not \u002Fuser\u002Ffoo\u002Fbar\n$r->addRoute('GET', '\u002Fuser\u002F{name}', 'handler');\n\n\u002F\u002F Matches \u002Fuser\u002Ffoo\u002Fbar as well\n$r->addRoute('GET', '\u002Fuser\u002F{name:.+}', 'handler');\n```\n\nCustom patterns for route placeholders cannot use capturing groups. For example `{lang:(en|de)}`\nis not a valid placeholder, because `()` is a capturing group. Instead you can use either\n`{lang:en|de}` or `{lang:(?:en|de)}`.\n\nFurthermore, parts of the route enclosed in `[...]` are considered optional, so that `\u002Ffoo[bar]`\nwill match both `\u002Ffoo` and `\u002Ffoobar`. Optional parts are only supported in a trailing position,\nnot in the middle of a route.\n\n```php\n\u002F\u002F This route\n$r->addRoute('GET', '\u002Fuser\u002F{id:\\d+}[\u002F{name}]', 'handler');\n\u002F\u002F Is equivalent to these two routes\n$r->addRoute('GET', '\u002Fuser\u002F{id:\\d+}', 'handler');\n$r->addRoute('GET', '\u002Fuser\u002F{id:\\d+}\u002F{name}', 'handler');\n\n\u002F\u002F Multiple nested optional parts are possible as well\n$r->addRoute('GET', '\u002Fuser[\u002F{id:\\d+}[\u002F{name}]]', 'handler');\n\n\u002F\u002F This route is NOT valid, because optional parts can only occur at the end\n$r->addRoute('GET', '\u002Fuser[\u002F{id:\\d+}]\u002F{name}', 'handler');\n```\n\nThe `$handler` parameter does not necessarily have to be a callback, it could also be a controller\nclass name or any other kind of data you wish to associate with the route. FastRoute only tells you\nwhich handler corresponds to your URI, how you interpret it is up to you.\n\n#### Shortcut methods for common request methods\n\nFor the `GET`, `POST`, `PUT`, `PATCH`, `DELETE` and `HEAD` request methods shortcut methods are available. For example:\n\n```php\n$r->get('\u002Fget-route', 'get_handler');\n$r->post('\u002Fpost-route', 'post_handler');\n```\n\nIs equivalent to:\n\n```php\n$r->addRoute('GET', '\u002Fget-route', 'get_handler');\n$r->addRoute('POST', '\u002Fpost-route', 'post_handler');\n```\n\n#### Route Groups\n\nAdditionally, you can specify routes inside a group. All routes defined inside a group will have a common prefix.\n\nFor example, defining your routes as:\n\n```php\n$r->addGroup('\u002Fadmin', function (FastRoute\\ConfigureRoutes $r) {\n    $r->addRoute('GET', '\u002Fdo-something', 'handler');\n    $r->addRoute('GET', '\u002Fdo-another-thing', 'handler');\n    $r->addRoute('GET', '\u002Fdo-something-else', 'handler');\n});\n```\n\nWill have the same result as:\n\n ```php\n$r->addRoute('GET', '\u002Fadmin\u002Fdo-something', 'handler');\n$r->addRoute('GET', '\u002Fadmin\u002Fdo-another-thing', 'handler');\n$r->addRoute('GET', '\u002Fadmin\u002Fdo-something-else', 'handler');\n ```\n\nNested groups are also supported, in which case the prefixes of all the nested groups are combined.\n\n### Caching\n\nThe reason `simpleDispatcher` accepts a callback for defining the routes is to allow seamless\ncaching. By using `cachedDispatcher` instead of `simpleDispatcher` you can cache the generated\nrouting data and construct the dispatcher from the cached information:\n\n```php\n\u003C?php\n\n$dispatcher = FastRoute\\cachedDispatcher(function(FastRoute\\ConfigureRoutes $r) {\n    $r->addRoute('GET', '\u002Fuser\u002F{name}\u002F{id:[0-9]+}', 'handler0');\n    $r->addRoute('GET', '\u002Fuser\u002F{id:[0-9]+}', 'handler1');\n    $r->addRoute('GET', '\u002Fuser\u002F{name}', 'handler2');\n}, [\n    'cacheKey' => __DIR__ . '\u002Froute.cache', \u002F* required *\u002F\n    \u002F\u002F 'cacheFile' => __DIR__ . '\u002Froute.cache', \u002F* will still work for v1 compatibility *\u002F\n    'cacheDisabled' => IS_DEBUG_ENABLED,     \u002F* optional, enabled by default *\u002F\n    'cacheDriver' => FastRoute\\Cache\\FileCache::class, \u002F* optional, class name or instance of the cache driver - defaults to file cache *\u002F\n]);\n```\n\nThe second parameter to the function is an options array, which can be used to specify the cache\nkey (e.g. file location when using files for caching), caching driver, among other things.\n\n### Dispatching a URI\n\nA URI is dispatched by calling the `dispatch()` method of the created dispatcher. This method\naccepts the HTTP method and a URI. Getting those two bits of information (and normalizing them\nappropriately) is your job - this library is not bound to the PHP web SAPIs.\n\nThe `dispatch()` method returns an array whose first element contains a status code. It is one\nof `Dispatcher::NOT_FOUND`, `Dispatcher::METHOD_NOT_ALLOWED` and `Dispatcher::FOUND`. For the\nmethod not allowed status the second array element contains a list of HTTP methods allowed for\nthe supplied URI. For example:\n\n    [FastRoute\\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'POST']]\n\n> **NOTE:** The HTTP specification requires that a `405 Method Not Allowed` response include the\n`Allow:` header to detail available methods for the requested resource. Applications using FastRoute\nshould use the second array element to add this header when relaying a 405 response.\n\nFor the found status the second array element is the handler that was associated with the route\nand the third array element is a dictionary of placeholder names to their values. For example:\n\n    \u002F* Routing against GET \u002Fuser\u002Fnikic\u002F42 *\u002F\n\n    [FastRoute\\Dispatcher::FOUND, 'handler0', ['name' => 'nikic', 'id' => '42']]\n\n### Overriding the route parser and dispatcher\n\nThe routing process makes use of three components: A route parser, a data generator and a\ndispatcher. The three components adhere to the following interfaces:\n\n```php\n\u003C?php\n\nnamespace FastRoute;\n\ninterface RouteParser {\n    public function parse($route);\n}\n\ninterface DataGenerator {\n    public function addRoute($httpMethod, $routeData, $handler);\n    public function getData();\n}\n\ninterface Dispatcher {\n    const NOT_FOUND = 0, FOUND = 1, METHOD_NOT_ALLOWED = 2;\n\n    public function dispatch($httpMethod, $uri);\n}\n```\n\nThe route parser takes a route pattern string and converts it into an array of route infos, where\neach route info is again an array of its parts. The structure is best understood using an example:\n\n    \u002F* The route \u002Fuser\u002F{id:\\d+}[\u002F{name}] converts to the following array: *\u002F\n    [\n        [\n            '\u002Fuser\u002F',\n            ['id', '\\d+'],\n        ],\n        [\n            '\u002Fuser\u002F',\n            ['id', '\\d+'],\n            '\u002F',\n            ['name', '[^\u002F]+'],\n        ],\n    ]\n\nThis array can then be passed to the `addRoute()` method of a data generator. After all routes have\nbeen added the `getData()` of the generator is invoked, which returns all the routing data required\nby the dispatcher. The format of this data is not further specified - it is tightly coupled to\nthe corresponding dispatcher.\n\nThe dispatcher accepts the routing data via a constructor and provides a `dispatch()` method, which\nyou're already familiar with.\n\nThe route parser can be overwritten individually (to make use of some different pattern syntax),\nhowever the data generator and dispatcher should always be changed as a pair, as the output from\nthe former is tightly coupled to the input of the latter. The reason the generator and the\ndispatcher are separate is that only the latter is needed when using caching (as the output of\nthe former is what is being cached.)\n\nWhen using the `simpleDispatcher` \u002F `cachedDispatcher` functions from above the override happens\nthrough the options array:\n\n```php\n\u003C?php\n\n$dispatcher = FastRoute\\simpleDispatcher(function(FastRoute\\ConfigureRoutes $r) {\n    \u002F* ... *\u002F\n}, [\n    'routeParser' => 'FastRoute\\\\RouteParser\\\\Std',\n    'dataGenerator' => 'FastRoute\\\\DataGenerator\\\\MarkBased',\n    'dispatcher' => 'FastRoute\\\\Dispatcher\\\\MarkBased',\n]);\n```\n\nThe above options array corresponds to the defaults. By replacing `MarkBased` with\n`GroupCountBased` you could switch to a different dispatching strategy.\n\n### A Note on HEAD Requests\n\nThe HTTP spec requires servers to [support both GET and HEAD methods][2616-511]:\n\n> The methods GET and HEAD MUST be supported by all general-purpose servers\n\nTo avoid forcing users to manually register HEAD routes for each resource we fallback to matching an\navailable GET route for a given resource. The PHP web SAPI transparently removes the entity body\nfrom HEAD responses so this behavior has no effect on the vast majority of users.\n\nHowever, implementers using FastRoute outside the web SAPI environment (e.g. a custom server) MUST\nNOT send entity bodies generated in response to HEAD requests. If you are a non-SAPI user this is\n*your responsibility*; FastRoute has no purview to prevent you from breaking HTTP in such cases.\n\nFinally, note that applications MAY always specify their own HEAD method route for a given\nresource to bypass this behavior entirely.\n\n### Credits\n\nThis library is based on a router that [Levi Morrison][levi] implemented for the Aerys server.\n\nA large number of tests, as well as HTTP compliance considerations, were provided by [Daniel Lowrey][rdlowrey].\n\n\n[2616-511]: http:\u002F\u002Fwww.w3.org\u002FProtocols\u002Frfc2616\u002Frfc2616-sec5.html#sec5.1.1 \"RFC 2616 Section 5.1.1\"\n[blog_post]: http:\u002F\u002Fnikic.github.io\u002F2014\u002F02\u002F18\u002FFast-request-routing-using-regular-expressions.html\n[levi]: https:\u002F\u002Fgithub.com\u002Fmorrisonlevi\n[rdlowrey]: https:\u002F\u002Fgithub.com\u002Frdlowrey\n","FastRoute 是一个为 PHP 设计的快速请求路由库。它基于正则表达式实现，提供了高效的路由匹配机制，能够显著提升应用处理 HTTP 请求的速度。FastRoute 支持定义复杂的路由规则，包括但不限于参数化路由和可选路径段，并且允许开发者通过简单的 API 来添加、管理和分发路由。此项目适用于需要高性能路由解析的各种 Web 应用场景，尤其是那些对响应时间有严格要求的应用。使用 Composer 可轻松集成 FastRoute 到现有 PHP 项目中，最低支持 PHP 8.1 版本。",2,"2026-06-11 03:17:27","top_language"]