{"id":13711922,"url":"https://github.com/httpsoft/http-router","last_synced_at":"2026-01-11T11:54:26.478Z","repository":{"id":56986522,"uuid":"294142961","full_name":"httpsoft/http-router","owner":"httpsoft","description":"Simple and fast HTTP request router providing PSR-7 and PSR-15","archived":false,"fork":false,"pushed_at":"2024-12-29T22:52:54.000Z","size":45,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-04T00:29:42.507Z","etag":null,"topics":["http","http-router","php","psr-15","psr-7","route","router"],"latest_commit_sha":null,"homepage":"https://httpsoft.org/docs/router","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/httpsoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-09T14:49:09.000Z","updated_at":"2024-12-29T22:49:49.000Z","dependencies_parsed_at":"2024-11-14T00:15:29.513Z","dependency_job_id":null,"html_url":"https://github.com/httpsoft/http-router","commit_stats":{"total_commits":35,"total_committers":1,"mean_commits":35.0,"dds":0.0,"last_synced_commit":"3c04e45654c30ccbd976371e8900f57489376ffb"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/httpsoft%2Fhttp-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/httpsoft%2Fhttp-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/httpsoft%2Fhttp-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/httpsoft%2Fhttp-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/httpsoft","download_url":"https://codeload.github.com/httpsoft/http-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252772200,"owners_count":21801872,"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":["http","http-router","php","psr-15","psr-7","route","router"],"created_at":"2024-08-02T23:01:12.985Z","updated_at":"2026-01-11T11:54:26.473Z","avatar_url":"https://github.com/httpsoft.png","language":"PHP","readme":"# HTTP Router\n\n[![License](https://poser.pugx.org/httpsoft/http-router/license)](https://packagist.org/packages/httpsoft/http-router)\n[![Latest Stable Version](https://poser.pugx.org/httpsoft/http-router/v)](https://packagist.org/packages/httpsoft/http-router)\n[![Total Downloads](https://poser.pugx.org/httpsoft/http-router/downloads)](https://packagist.org/packages/httpsoft/http-router)\n[![GitHub Build Status](https://github.com/httpsoft/http-router/workflows/build/badge.svg)](https://github.com/httpsoft/http-router/actions)\n[![GitHub Static Analysis Status](https://github.com/httpsoft/http-router/workflows/static/badge.svg)](https://github.com/httpsoft/http-router/actions)\n[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/httpsoft/http-router/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/httpsoft/http-router/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/httpsoft/http-router/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/httpsoft/http-router/?branch=master)\n\nThis package provides convenient management of HTTP request routing with support for [PSR-7](https://github.com/php-fig/http-message) and [PSR-15](https://github.com/php-fig/http-factory).\n\n## Documentation\n\n* [In English language](https://httpsoft.org/docs/router).\n* [In Russian language](https://httpsoft.org/ru/docs/router).\n\n## Installation\n\nThis package requires PHP version 7.4 or later.\n\n```\ncomposer require httpsoft/http-router\n```\n\n## Usage\n\n```php\nuse HttpSoft\\Router\\RouteCollector;\n\n/**\n * @var mixed $handler\n */\n\n$router = new RouteCollector();\n\n// Defining routes.\n$router-\u003eget('home', '/', $handler);\n$router-\u003epost('logout', '/logout', $handler);\n$router-\u003eadd('login', '/login', $handler, ['GET', 'POST']);\n\n// Custom regular expressions for placeholder parameter tokens.\n$router-\u003edelete('post.delete', '/post/delete/{id}', $handler)-\u003etokens(['id' =\u003e '\\d+']);\n\n// Generate path '/post/delete/25'\n$router-\u003eroutes()-\u003epath('post.delete', ['id' =\u003e 25]);\n// Generate url '//example.com/post/delete/25'\n$router-\u003eroutes()-\u003eurl('post.delete', ['id' =\u003e 25], 'example.com');\n// Generate url 'https://example.com/post/delete/25'\n$router-\u003eroutes()-\u003eurl('post.delete', ['id' =\u003e 25], 'example.com', true);\n```\n\nSet the parameter to the default value.\n\n```php\n$router-\u003eget('post.view', '/post/{slug}{format}', $handler)\n    -\u003etokens(['slug' =\u003e '[\\w\\-]+', 'format' =\u003e '\\.[a-zA-z]{3,}'])\n    -\u003edefaults(['format' =\u003e '.html'])\n;\n\n// Generate path '/post/post-slug.html'.\n$router-\u003eroutes()-\u003epath('post.view', ['slug' =\u003e 'post-slug']);\n```\n\nTokens of the route enclosed in `[...]` are considered optional.\n\n```php\n$router-\u003eget('post.list', '/posts{[page]}', $handler)\n    -\u003etokens(['page' =\u003e '\\d+'])\n;\n\n// '/posts/33'\n$router-\u003eroutes()-\u003epath('post.list', ['page' =\u003e 33]);\n// '/posts'\n$router-\u003eroutes()-\u003epath('post.list');\n```\n\nIf necessary, you can specify a specific host for route matching.\n\n```php\n// Only for example.com\n$router-\u003eget('page', '/page', $handler)\n    -\u003ehost('example.com')\n;\n\n// Only for subdomain.example.com\n$router-\u003eget('page', '/page', $handler)\n    -\u003ehost('subdomain.example.com')\n;\n\n// Only for shop.example.com or blog.example.com\n$router-\u003eget('page', '/page', $handler)\n    -\u003ehost('(shop|blog).example.com')\n;\n```\n\nYou can specify routes inside of a group.\n\n```php\n$router-\u003egroup('/post', static function (RouteCollector $router): void {\n    // '/post/post-slug'\n    $router-\u003eget('post.view', '/{slug}', ViewHandler::class)-\u003etokens(['slug' =\u003e '[\\w-]+']);\n    // '/post' or '/post/2'\n    $router-\u003eget('post.list', '/list{[page]}', ListHandler::class)-\u003etokens(['page' =\u003e '\\d+']);\n});\n\n// The result will be equivalent to:\n\n$router-\u003eget('post.view', '/post/{slug}', ViewHandler::class)-\u003etokens(['slug' =\u003e '[\\w-]+']);\n$router-\u003eget('post.list', '/post/list{[page]}', ListHandler::class)-\u003etokens(['page' =\u003e '\\d+']);\n```\n\nCheck matching routes.\n\n```php\n/**\n * @var mixed $handler\n * @var Psr\\Http\\Message\\UriInterface $uri\n * @var Psr\\Http\\Message\\ServerRequestInterface $request\n */\n\n$router-\u003eget('page', '/page/{id}', $handler)-\u003etokens(['id' =\u003e '\\d+']);\n\n// Match\n$route = $router-\u003eroutes()-\u003ematch($request-\u003ewithUri($uri-\u003ewithPath('/page/11')));\n$route-\u003egetMatchedParameters(); // ['id' =\u003e '11']\n\n// Mismatch\n$router-\u003eroutes()-\u003ematch($request-\u003ewithUri($uri-\u003ewithPath('/page/slug'))); // null\n```\n","funding_links":[],"categories":["Packages"],"sub_categories":["Router"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhttpsoft%2Fhttp-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhttpsoft%2Fhttp-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhttpsoft%2Fhttp-router/lists"}