https://github.com/freeelephants/psr-router
PSR router
https://github.com/freeelephants/psr-router
framework-agnostic http-router php-router psr-15 psr-7 routing
Last synced: 5 months ago
JSON representation
PSR router
- Host: GitHub
- URL: https://github.com/freeelephants/psr-router
- Owner: FreeElephants
- License: bsd-2-clause
- Created: 2025-06-10T16:20:38.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-06-26T15:42:53.000Z (about 1 year ago)
- Last Synced: 2025-06-26T16:39:09.695Z (about 1 year ago)
- Topics: framework-agnostic, http-router, php-router, psr-15, psr-7, routing
- Language: PHP
- Homepage:
- Size: 37.1 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Framework Agnostic PSR Router
## Usage
```php
$routes = [
// 1. "flat" syntax - single level configuration
'/api/v1/users' => GetUsersHandler::class, // by default GET method ar user for single value
// 2. Method-based syntax - two levels configuration
'/api/v1/users/{id}' => [ // Router parse path params and set named arguments to request (method based syntax
'GET' => GetUserHandler::class,
'PATCH' => UpdateUserHandler::class,
'DELETE' => new class implements \Psr\Http\Server\RequestHandlerInterface {
// psr handler instance allowed as value too
}
],
];
$fastRouteDispatcher = (new \FreeElephants\PsrRouter\FastRoute\DispatcherBuilder())
->addConfig($routes)
->addRoute('/foo', FooHandler::class) // You can define routes by one
->build();
$router = new \FreeElephants\PsrRouter\Router(
$fastRouteDispatcher,
new \FreeElephants\PsrRouter\RequestHandlerFactory($psrContainer),
);
```
## Customization
### Slash Normalizing
For handle trailing slashes in path you can inject PathNormalizer implementation into both classes:
- Router - its rtrim trailing slash at runtime from request path.
- DispatcherBuilder - its rtrim on add route at build time.
```php
$pathNormalizer = new \FreeElephants\PsrRouter\PathNormalization\TrailingSlashTrimmer();
$dispatcher = (new \FreeElephants\PsrRouter\FastRoute\DispatcherBuilder(
pathNormalizer: $pathNormalizer))
...
...
->build();
$router = new \FreeElephants\PsrRouter\Router(
$dispatcher,
$requestHandlerFactory,
pathNormalizer: $pathNormalizer
)
```
### OPTIONS Handling
By default, Router does not know about `OPTIONS` and allowed methods for route.
For handle `OPTIONS` request, you can set your own handler prototype. Every route will be handling this method according to other collected methods.
```php
/**
* @var \FreeElephants\PsrRouter\FastRoute\DispatcherBuilder $dispatcherBuilder
*/
$dispatcherBuilder->setOptionsHandlerPrototype($optionsRequestHandlerImpl)->build();
```
## Middleware Configuration Support
```php
$middleware = (new \FreeElephants\Middleware\Laminas\MiddlewareBuilder($container))
->setDefaultMethods(['GET', 'POST']) // see MiddlewareBuilder::DEFAULT_METHODS
->addConfig([
// Examples for different levels of configuration, anywhere path based
'/' => RootMiddlewareForAllDefaultMethods::class,
'/foo' => [
FooMiddlewareA::class,
FooMiddlewareB::class,
],
'/foo/{arg}' => [
'PUT' => [
PutFooMiddleware::class,
],
'OPTIONS' => FooWithArgOptionsMiddleware::class,
],
])
->build();
$middleware->process($request, $routerOrYourOwnedRequestHandler);
```