{"id":19665911,"url":"https://github.com/amphp/http-server-router","last_synced_at":"2025-10-13T09:36:01.534Z","repository":{"id":56947327,"uuid":"124802098","full_name":"amphp/http-server-router","owner":"amphp","description":"A router for Amp's HTTP Server.","archived":false,"fork":false,"pushed_at":"2024-09-15T16:00:46.000Z","size":80,"stargazers_count":39,"open_issues_count":1,"forks_count":6,"subscribers_count":8,"default_branch":"2.x","last_synced_at":"2025-09-16T18:01:40.861Z","etag":null,"topics":["amphp","fast-route","http","php","router"],"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/amphp.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-11T21:41:25.000Z","updated_at":"2025-04-08T17:01:26.000Z","dependencies_parsed_at":"2023-11-26T18:23:09.634Z","dependency_job_id":"6deb1e9e-b7c3-4dcc-ac97-0bcad0fa6bed","html_url":"https://github.com/amphp/http-server-router","commit_stats":{"total_commits":26,"total_committers":3,"mean_commits":8.666666666666666,"dds":"0.42307692307692313","last_synced_commit":"d40982cbfb130bbcfafcadf9c0b5b5692fe1db94"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/amphp/http-server-router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amphp","download_url":"https://codeload.github.com/amphp/http-server-router/tar.gz/refs/heads/2.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-router/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279014544,"owners_count":26085536,"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","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["amphp","fast-route","http","php","router"],"created_at":"2024-11-11T16:25:25.641Z","updated_at":"2025-10-13T09:36:01.517Z","avatar_url":"https://github.com/amphp.png","language":"PHP","readme":"# http-server-router\n\nAMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.\nThis package provides a routing `RequestHandler` for [`amphp/http-server`](https://github.com/amphp/http-server) based on the request path and method using [FastRoute](https://github.com/nikic/FastRoute).\n\n## Installation\n\nThis package can be installed as a [Composer](https://getcomposer.org/) dependency.\nYou should specify `amphp/http-server` as a separate dependency.\n\n```bash\ncomposer require amphp/http-server-router\n```\n\n## Usage\n\n**`Router`** implements `RequestHandler`, which is used by an [`HttpServer`](https://github.com/amphp/http-server#creating-an-http-server) to handle incoming requests. Incoming requests are routed by `Router` to other `RequestHandler`s based on the request path.\n\nRoutes can be defined using the `addRoute($method, $uri, $requestHandler)` method.\n\n```php\npublic function addRoute(\n    string $method,\n    string $uri,\n    RequestHandler $requestHandler\n): void\n```\n\nMatched route arguments are available in the request attributes under the `Amp\\Http\\Server\\Router` key as an associative array.\nPlease read the [FastRoute documentation on how to define placeholders](https://github.com/nikic/FastRoute#defining-routes).\n\n### Middleware\n\nYou can stack all routes with a common set of middleware using `addMiddleware($middleware)`.\nEach middleware is called in the order of the `addMiddleware()` calls.\nThe router will not invoke any middleware for the fallback handler.\n\n```php\npublic function addMiddleware(Middleware $middleware): void\n```\n\n\u003e **Note**\n\u003e Per-route middleware can be added by using `Amp\\Http\\Server\\Middleware\\stackMiddleware()` before passing the `RequestHandler` to `addRoute()`.\n\n### Fallback\n\nIf no routes match a request path, you can specify another instance of `RequestHandler` which will handle any unmatched routes. If no fallback handler is provided, a 404 response will be returned using the instance of `ErrorHandler` provided to the `Router` constructor.\n\n```php\npublic function setFallback(RequestHandler $requestHandler): void\n```\n\n\u003e **Note**\n\u003e Middleware defined by `Router::addMiddleware()` will _not_ be invoked when a request is forwarded to fallback handler. Use `Amp\\Http\\Server\\Middleware\\stackMiddleware()` to wrap the fallback request handler with any middlewares needed first.\n\n## Example\n\n```php\nuse Amp\\Http\\HttpStatus;\nuse Amp\\Http\\Server\\DefaultErrorHandler;\nuse Amp\\Http\\Server\\Request;\nuse Amp\\Http\\Server\\RequestHandler\\ClosureRequestHandler;\nuse Amp\\Http\\Server\\Response;\nuse Amp\\Http\\Server\\Router;\nuse Amp\\Http\\Server\\SocketHttpServer;\n\n// $logger is an instance of a PSR-3 logger.\n$server = SocketHttpServer::createForDirectAccess($logger);\n$errorHandler = new DefaultErrorHandler();\n\n$router = new Router($server, $logger, $errorHandler);\n\n$router-\u003eaddRoute('GET', '/', new ClosureRequestHandler(\n    function () {\n        return new Response(\n            status: HttpStatus::OK,\n            headers: ['content-type' =\u003e 'text/plain'],\n            body: 'Hello, world!',\n        );\n    },\n));\n\n$router-\u003eaddRoute('GET', '/{name}', new ClosureRequestHandler(\n    function (Request $request) {\n        $args = $request-\u003egetAttribute(Router::class);\n        return new Response(\n            status: HttpStatus::OK,\n            headers: ['content-type' =\u003e 'text/plain'],\n            body: \"Hello, {$args['name']}!\",\n        );\n    },\n));\n\n$server-\u003eexpose('0.0.0.0:1337');\n\n$server-\u003estart($router, $errorHandler);\n\n// Serve requests until SIGINT or SIGTERM is received by the process.\nAmp\\trapSignal([SIGINT, SIGTERM]);\n\n$server-\u003estop();\n```\n\nA full example is found in [`examples/hello-world.php`](https://github.com/amphp/http-server-router/blob/2.x/examples/hello-world.php).\n\n```bash\nphp examples/hello-world.php\n```\n\n## Limitations\n\nThe `Router` will decode the URI path before matching.\nThis will also decode any forward slashes (`/`), which might result in unexpected matching for URIs with encoded slashes.\nFastRoute placeholders match path segments by default, which are separated by slashes.\nThat means a route like `/token/{token}` won't match if the token contains an encoded slash.\nYou can work around this limitation by using a custom regular expression for the placeholder like `/token/{token:.+}`.\n\n## Security\n\nIf you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.\n\n## License\n\nThe MIT License (MIT). Please see [LICENSE](./LICENSE) for more information.\n","funding_links":[],"categories":["HTTP"],"sub_categories":["Routing"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famphp%2Fhttp-server-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famphp%2Fhttp-server-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famphp%2Fhttp-server-router/lists"}