{"id":33951911,"url":"https://github.com/northwoods/router","last_synced_at":"2025-12-12T19:12:26.224Z","repository":{"id":57028503,"uuid":"156318292","full_name":"northwoods/router","owner":"northwoods","description":"Fast router for PSR-15 request handlers","archived":false,"fork":false,"pushed_at":"2018-12-17T15:01:40.000Z","size":19,"stargazers_count":16,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-13T20:42:16.829Z","etag":null,"topics":["handler","http","psr-15","request","server"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/northwoods.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-06T03:07:57.000Z","updated_at":"2025-02-16T04:27:34.000Z","dependencies_parsed_at":"2022-08-23T18:50:07.360Z","dependency_job_id":null,"html_url":"https://github.com/northwoods/router","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/northwoods/router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/northwoods%2Frouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/northwoods%2Frouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/northwoods%2Frouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/northwoods%2Frouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/northwoods","download_url":"https://codeload.github.com/northwoods/router/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/northwoods%2Frouter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27689218,"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-12-12T02:00:06.775Z","response_time":129,"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":["handler","http","psr-15","request","server"],"created_at":"2025-12-12T19:12:25.576Z","updated_at":"2025-12-12T19:12:26.217Z","avatar_url":"https://github.com/northwoods.png","language":"PHP","readme":"# Northwoods Router\n\n[![Build Status](https://travis-ci.com/northwoods/router.svg?branch=master)](https://travis-ci.com/northwoods/router)\n[![Code Quality](https://scrutinizer-ci.com/g/northwoods/router/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/northwoods/router/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/northwoods/router/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/northwoods/router/?branch=master)\n[![Latest Stable Version](http://img.shields.io/packagist/v/northwoods/router.svg?style=flat)](https://packagist.org/packages/northwoods/router)\n[![Total Downloads](https://img.shields.io/packagist/dt/northwoods/router.svg?style=flat)](https://packagist.org/packages/northwoods/router)\n[![License](https://img.shields.io/packagist/l/northwoods/router.svg?style=flat)](https://packagist.org/packages/northwoods/router)\n\nA [FastRoute][fastroute] based router designed to be used with [PSR-15 middleware][psr15].\n\n[fastroute]: https://github.com/nikic/FastRoute\n[psr15]: https://www.php-fig.org/psr/psr-15/\n\n## Installation\n\nThe best way to install and use this package is with [composer](http://getcomposer.org/):\n\n```shell\ncomposer require northwoods/router\n```\n\n## Usage\n\nThe router implements `MiddlewareInterface` and can be used with any middleware\ndispatcher, such as [Broker][broker].\n\n```php\nuse Northwoods\\Router\\Router;\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Psr\\Http\\Message\\ServerRequestInterface;\n\n$router = new Router();\n$router-\u003eget('user.list', '/users', $userList);\n$router-\u003eget('user.detail', '/users/{id:\\d+}', $userDetail);\n$router-\u003epost('user.create', '/users', $userCreate);\n\nassert($router instanceof Psr\\Http\\Server\\MiddlewareInterface);\n```\n\nThis is the preferred usage of the router, as it ensures that the request is\nproperly set up for the route handler. Generally the router should be the last\nmiddleware in the stack.\n\nIf you prefer to use the router without middleware, the router also implements\n`RequestHandlerInterface` and can be used directly:\n\n```php\n/** @var ServerRequestInterface */\n$request = /* create server request */;\n\n/** @var ResponseInterface */\n$response = $router-\u003ehandle($request);\n```\n\n[broker]: https://github.com/northwoods/broker\n\n### Route Handlers\n\nAll route handlers MUST implement the `RequestHandlerInterface` interface:\n\n```php\nnamespace Acme;\n\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse Psr\\Http\\Server\\RequestHandlerInterface;\n\nclass UserListHandler implements RequestHandlerInterface\n{\n    public function handle(ServerRequestInterface $request): ResponseInterface\n    {\n        /** @var array */\n        $users = /* load from database, etc */;\n\n        return new Response(200, ['content-type' =\u003e 'application-json'], json_encode($users));\n    }\n}\n```\n\nIf it is preferable to lazy load handlers, the [lazy-middleware][lazy-middleware]\npackage can be used:\n\n```php\nuse Northwoods\\Middleware\\LazyHandlerFactory;\n\n/** @var LazyHandlerFactory */\n$lazyHandler = /* create the factory */;\n\n$router-\u003epost('user.create', '/users', $lazyHandler-\u003edefer(CreateUserHandler::class));\n```\n\n[lazy-middleware]: https://github.com/northwoods/lazy-middleware\n\n### Reverse Routing\n\nReverse routing enables generating URIs from routes:\n\n```php\n$uri = $router-\u003euri('user.detail', ['id' =\u003e 100]);\n\nassert($uri === '/users/100');\n```\n\n## API\n\n### Router::add($name, $route);\n\nAdd a fully constructed route.\n\n### Router::get($name, $pattern, $handler)\n\nAdd a route that works for HTTP GET requests.\n\n### Router::post($name, $pattern, $handler)\n\nAdd a route that works for HTTP POST requests.\n\n### Router::put($name, $pattern, $handler)\n\nAdd a route that works for HTTP PUT requests.\n\n### Router::patch($name, $pattern, $handler)\n\nAdd a route that works for HTTP PATCH requests.\n\n### Router::delete($name, $pattern, $handler)\n\nAdd a route that works for HTTP DELETE requests.\n\n### Router::head($name, $pattern, $handler)\n\nAdd a route that works for HTTP HEAD requests.\n\n### Router::options($name, $pattern, $handler)\n\nAdd a route that works for HTTP OPTIONS requests.\n\n### Router::process($request, $handler)\n\nDispatch routing as a middleware.\n\nIf no route is found, the `$handler` will be used to generate the response.\n\n### Router::handle($request)\n\nDispatch routing for a request.\n\nIf no route is found, a response with a HTTP 404 status will be returned.\n\nIf a route is found, but it does not allow the request method, a response with\na HTTP 405 will be returned.\n\n## Credits\n\nBorrows some ideas from [zend-expressive-fastroute][zf-fastroute] for handling [reverse routing][zf-rr].\n\n[zf-fastroute]: https://github.com/zendframework/zend-expressive-fastroute\n[zf-rr]: https://github.com/zendframework/zend-expressive-fastroute/pull/32\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnorthwoods%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnorthwoods%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnorthwoods%2Frouter/lists"}