{"id":13755515,"url":"https://github.com/fastdlabs/routing","last_synced_at":"2026-01-11T16:47:15.792Z","repository":{"id":32350190,"uuid":"35925858","full_name":"fastdlabs/routing","owner":"fastdlabs","description":"FastD Routing.","archived":false,"fork":false,"pushed_at":"2023-10-24T13:57:47.000Z","size":443,"stargazers_count":15,"open_issues_count":1,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-17T14:19:16.861Z","etag":null,"topics":["php","restful","routing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastdlabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-05-20T04:31:47.000Z","updated_at":"2024-12-16T07:19:37.000Z","dependencies_parsed_at":"2022-09-12T05:12:35.417Z","dependency_job_id":"5579da02-0173-42d2-976a-eab4be6300c3","html_url":"https://github.com/fastdlabs/routing","commit_stats":{"total_commits":277,"total_committers":11,"mean_commits":"25.181818181818183","dds":"0.43321299638989175","last_synced_commit":"8399013643ee496ca70aab4a60a9446b5fcc3cc6"},"previous_names":["janhuang/routing"],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastdlabs%2Frouting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastdlabs%2Frouting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastdlabs%2Frouting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastdlabs%2Frouting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastdlabs","download_url":"https://codeload.github.com/fastdlabs/routing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253349977,"owners_count":21894813,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","restful","routing"],"created_at":"2024-08-03T10:00:55.625Z","updated_at":"2026-01-11T16:47:15.763Z","avatar_url":"https://github.com/fastdlabs.png","language":"PHP","funding_links":[],"categories":["PHP 相关"],"sub_categories":[],"readme":"# FastD Routing\n\n[![Build Status](https://travis-ci.org/fastdlabs/routing.svg?branch=master)](https://travis-ci.org/fastdlabs/routing)\n[![Latest Stable Version](https://poser.pugx.org/fastd/routing/v/stable)](https://packagist.org/packages/fastd/routing)\n[![Total Downloads](https://poser.pugx.org/fastd/routing/downloads)](https://packagist.org/packages/fastd/routing)\n[![Latest Unstable Version](https://poser.pugx.org/fastd/routing/v/unstable)](https://packagist.org/packages/fastd/routing)\n[![License](https://poser.pugx.org/fastd/routing/license)](https://packagist.org/packages/fastd/routing)\n\nSimple PHP router that supports routing nesting, dynamic routing, fuzzy routing, middleware, and more. Relies on the [http](https://github.com/JanHuang/http) component.\n\n## Claim\n\n* PHP 7.2\n\n## Composer\n\n```\nComposer require \"fastd/routing\"\n```\n\n## Use\n\nYou can set the route through the `RouteCollection` object, or you can create a route through the route list. Detailed documentation: [fastd/routing](docs/zh_CN/readme.md)\n\n### Static routing\n\n```php\nuse FastD\\Http\\ServerRequest;\nuse FastD\\Routing\\RouteCollection;\n\n$collection = new RouteCollection();\n\n$collection-\u003eaddRoute('GET', '/', function () {\n    return 'hello world';\n});\n\n$route = $collection-\u003ematch(new ServerRequest('GET', '/')); // \\FastD\\Routing\\Route\n\necho call_user_func_array($route-\u003egetCallback(), []);\n```\n\nRoute matching does not call the callback of the route, but returns the entire Route for callback processing, so `match` only returns the matching route object.\n\n### Dynamic routing\n\n```php\nuse FastD\\Http\\ServerRequest;\nuse FastD\\Routing\\RouteCollection;\n\n$collection = new RouteCollection();\n\n$collection-\u003eaddRoute('GET', '/{name}', function ($name) {\n    return 'hello ' . $name;\n});\n\n$route = $collection-\u003ematch(new ServerRequest('GET', '/foo')); // \\FastD\\Routing\\Route\n\necho call_user_func_array($route-\u003egetCallback(), $route-\u003egetParameters());\n```\n\nUnder dynamic routing, the successfully matched route will update the matching parameters to `getParameters`, and get the matching parameter information through `getParameters`.\n\n### Same route, multiple methods\n\n```php\n$collection = new FastD\\Routing\\RouteCollection();\n$collection-\u003eget('/', function () {\n    return 'hello GET';\n});\n$collection-\u003epost('/', function () {\n    return 'hello POST';\n});\n$response = $collection-\u003edispatch('GET', '/'); // hello GET\n$response = $collection-\u003edispatch('POST', '/'); // hello POST\n```\n\n### Hybrid routing\n\nIn many cases, our route may only be one parameter difference. Here is an example.\n\n```php\nuse FastD\\Http\\ServerRequest;\nuse FastD\\Routing\\RouteCollection;\n\n$collection = new RouteCollection();\n\n$collection-\u003eaddRoute('GET', '/{name}', function () {\n    return 'get1';\n});\n\n$collection-\u003eaddRoute('GET', '/', function () {\n    return 'get2';\n});\n\n$route = $collection-\u003ematch(new ServerRequest('GET', '/abc')); // \\FastD\\Routing\\Route\n$route2 = $collection-\u003ematch(new ServerRequest('GET', '/')); // \\FastD\\Routing\\Route\necho call_user_func_array($route-\u003egetCallback(), $route-\u003egetParameters());\necho call_user_func_array($route2-\u003egetCallback(), $route2-\u003egetParameters());\n```\n\n### Routing Group\n\nThe routing group will add its own routing prefix to each of your sub-routing dollars, supporting multiple levels of nesting.\n\n```php\nuse FastD\\Http\\ServerRequest;\nuse FastD\\Routing\\RouteCollection;\n\n$collection = new RouteCollection();\n\n$collection-\u003egroup('/v1', function (RouteCollection $collection) {\n    $collection-\u003eaddRoute('GET', '/{name}', function () {\n        return 'get1';\n    });\n});\n\n$route = $collection-\u003ematch(new ServerRequest('GET', '/v1/abc'));\n\necho call_user_func_array($route-\u003egetCallback(), $route-\u003egetParameters());\n```\n\n### Fuzzy routing\n\nThe inspiration for fuzzy routing comes from the onRequest callback of the Swoole http server. Because each route entry passes onRequest, when it is created, there may be some special routes processed according to pathinfo. Then the fuzzy route can be sent. It’s time to use.\n\n```php\nuse FastD\\Http\\ServerRequest;\nuse FastD\\Routing\\RouteCollection;\n\n$collection = new RouteCollection();\n\n$collection-\u003eaddRoute('GET', '/api/*', function ($path) {\n    return $path;\n});\n\n$route = $collection-\u003ematch(new ServerRequest('GET', '/api/abc'));\necho call_user_func_array($route-\u003egetCallback(), $route-\u003egetParameters()); // /abc\n\n$route = $collection-\u003ematch(new ServerRequest('GET', '/api/cba'));\necho call_user_func_array($route-\u003egetCallback(), $route-\u003egetParameters()); // /cba\n```\n\nMatch all legal routes that start with `/api` and then callback\n\n### Routing middleware\n\nThe routing component implements routing middleware based on [Http] (https://github.com/JanHuang/http) and [HTTP Middlewares] (https://github.com/JanHuang/middleware).\n\n\u003e Routing middleware callbacks automatically call back `Psr\\Http\\Message\\ServerRequestInterface` and `FastD\\Middleware\\DelegateInterface` as arguments.\n\nAfter the middleware call is completed, the `\\Psr\\Http\\Message\\ResponseInterface` object is returned for the program to process the output.\n\n```php\nuse FastD\\Http\\ServerRequest;\nuse FastD\\Routing\\RouteCollection;\n\n$collection = new RouteCollection();\n\n$collection-\u003eaddRoute('GET', '/api/*', function (ServerRequest $request) {\n    return 'hello';\n});\n\n$dispatcher = new \\FastD\\Routing\\RouteDispatcher($collection);\n\n$response = $dispatcher-\u003edispatch(new ServerRequest('GET', '/api/abc'));\n\necho $response-\u003egetBody();\n```\n\n## Testing\n\n```\nphpunit\n```\n\n### Contribution\n\nI am very pleased to be interested and willing to participate in the creation of a better PHP ecosystem, the developer of the Swoole Eco.\n\nIf you are happy with this, but don't know how to get started, you can try the following things:\n\n* Problems encountered in your system [Feedback] (https://github.com/JanHuang/fastD/issues).\n* Have better suggestions? Feel free to contact [bboyjanhuang@gmail.com] (mailto:bboyjanhuang@gmail.com) or [Sina Weibo: Coding Man] (http://weibo.com/ecbboyjan).\n\n### Contact\n\nIf you encounter problems during use, please contact: [bboyjanhuang@gmail.com](mailto:bboyjanhuang@gmail.com). Weibo: [Coding Man] (http://weibo.com/ecbboyjan)\n\n## License MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastdlabs%2Frouting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastdlabs%2Frouting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastdlabs%2Frouting/lists"}