{"id":28779491,"url":"https://github.com/freeelephants/psr-router","last_synced_at":"2026-01-20T17:29:38.282Z","repository":{"id":298350230,"uuid":"999686118","full_name":"FreeElephants/psr-router","owner":"FreeElephants","description":"PSR router","archived":false,"fork":false,"pushed_at":"2025-06-26T15:42:53.000Z","size":38,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-26T16:39:09.695Z","etag":null,"topics":["framework-agnostic","http-router","php-router","psr-15","psr-7","routing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FreeElephants.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-10T16:20:38.000Z","updated_at":"2025-06-24T15:47:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"5b7821cd-478b-469d-b651-54a7d10286d2","html_url":"https://github.com/FreeElephants/psr-router","commit_stats":null,"previous_names":["freeelephants/psr-router"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/FreeElephants/psr-router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FreeElephants%2Fpsr-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FreeElephants%2Fpsr-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FreeElephants%2Fpsr-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FreeElephants%2Fpsr-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FreeElephants","download_url":"https://codeload.github.com/FreeElephants/psr-router/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FreeElephants%2Fpsr-router/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262108168,"owners_count":23260296,"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":["framework-agnostic","http-router","php-router","psr-15","psr-7","routing"],"created_at":"2025-06-17T17:05:55.510Z","updated_at":"2026-01-20T17:29:38.232Z","avatar_url":"https://github.com/FreeElephants.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Framework Agnostic PSR Router\n\n## Usage\n\n```php\n\n$routes = [\n    // 1. \"flat\" syntax - single level configuration\n    '/api/v1/users' =\u003e GetUsersHandler::class, // by default GET method ar user for single value\n    // 2. Method-based syntax - two levels configuration\n    '/api/v1/users/{id}' =\u003e [ // Router parse path params and set named arguments to request (method based syntax\n        'GET' =\u003e GetUserHandler::class,\n        'PATCH' =\u003e UpdateUserHandler::class,\n        'DELETE' =\u003e new class implements \\Psr\\Http\\Server\\RequestHandlerInterface {\n            // psr handler instance allowed as value too \n        }\n    ],\n];\n\n$fastRouteDispatcher = (new \\FreeElephants\\PsrRouter\\FastRoute\\DispatcherBuilder())\n    -\u003eaddConfig($routes)\n    -\u003eaddRoute('/foo', FooHandler::class) // You can define routes by one\n    -\u003ebuild(); \n\n$router = new \\FreeElephants\\PsrRouter\\Router(\n    $fastRouteDispatcher,\n    new \\FreeElephants\\PsrRouter\\RequestHandlerFactory($psrContainer),\n);\n\n```\n\n## Customization\n\n### Slash Normalizing\n\nFor handle trailing slashes in path you can inject PathNormalizer implementation into both classes:  \n- Router - its rtrim trailing slash at runtime from request path.  \n- DispatcherBuilder - its rtrim on add route at build time.\n\n```php\n$pathNormalizer = new \\FreeElephants\\PsrRouter\\PathNormalization\\TrailingSlashTrimmer();\n$dispatcher = (new \\FreeElephants\\PsrRouter\\FastRoute\\DispatcherBuilder(\n    pathNormalizer: $pathNormalizer))\n    ...\n    ...\n    -\u003ebuild();\n\n\n$router = new \\FreeElephants\\PsrRouter\\Router(\n    $dispatcher,\n    $requestHandlerFactory,\n    pathNormalizer: $pathNormalizer\n    \n)\n```\n\n### OPTIONS Handling  \n\nBy default, Router does not know about `OPTIONS` and allowed methods for route. \n\nFor handle `OPTIONS` request, you can set your own handler prototype. Every route will be handling this method according to other collected methods.  \n\n```php\n/**\n * @var \\FreeElephants\\PsrRouter\\FastRoute\\DispatcherBuilder $dispatcherBuilder\n */\n$dispatcherBuilder-\u003esetOptionsHandlerPrototype($optionsRequestHandlerImpl)-\u003ebuild();\n```\n\n## Middleware Configuration Support\n\n```php\n$middleware = (new \\FreeElephants\\Middleware\\Laminas\\MiddlewareBuilder($container))\n    -\u003esetDefaultMethods(['GET', 'POST']) // see MiddlewareBuilder::DEFAULT_METHODS\n    -\u003eaddConfig([\n        // Examples for different levels of configuration, anywhere path based\n        '/' =\u003e RootMiddlewareForAllDefaultMethods::class,\n        '/foo' =\u003e [\n            FooMiddlewareA::class,\n            FooMiddlewareB::class,\n        ], \n        '/foo/{arg}' =\u003e [\n            'PUT' =\u003e [\n                PutFooMiddleware::class,\n            ],\n            'OPTIONS' =\u003e FooWithArgOptionsMiddleware::class,\n        ],   \n    ])\n    -\u003ebuild();\n\n$middleware-\u003eprocess($request, $routerOrYourOwnedRequestHandler);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreeelephants%2Fpsr-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffreeelephants%2Fpsr-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreeelephants%2Fpsr-router/lists"}