PHPackages                             free-elephants/psr-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. free-elephants/psr-router

ActiveLibrary[Framework](/categories/framework)

free-elephants/psr-router
=========================

Framework Agnostic PSR Router

0.0.6(10mo ago)3843↓33.3%BSD-2-ClausePHPCI passing

Since Jun 11Pushed 10mo agoCompare

[ Source](https://github.com/FreeElephants/psr-router)[ Packagist](https://packagist.org/packages/free-elephants/psr-router)[ RSS](/packages/free-elephants-psr-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (10)Versions (10)Used By (0)

Framework Agnostic PSR Router
=============================

[](#framework-agnostic-psr-router)

Usage
-----

[](#usage)

```
$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
-------------

[](#customization)

### Slash Normalizing

[](#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.

```
$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

[](#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.

```
/**
 * @var \FreeElephants\PsrRouter\FastRoute\DispatcherBuilder $dispatcherBuilder
 */
$dispatcherBuilder->setOptionsHandlerPrototype($optionsRequestHandlerImpl)->build();
```

Middleware Configuration Support
--------------------------------

[](#middleware-configuration-support)

```
$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);
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance54

Moderate activity, may be stable

Popularity22

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity32

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

6

Last Release

320d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b764f6cd1eb52054cf0e07297309c60e37382542268822b3bd5edd97729d056?d=identicon)[samizdam](/maintainers/samizdam)

---

Top Contributors

[![samizdam](https://avatars.githubusercontent.com/u/1556299?v=4)](https://github.com/samizdam "samizdam (13 commits)")

---

Tags

framework-agnostichttp-routerphp-routerpsr-15psr-7routing

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/free-elephants-psr-router/health.svg)

```
[![Health](https://phpackages.com/badges/free-elephants-psr-router/health.svg)](https://phpackages.com/packages/free-elephants-psr-router)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[cakephp/authentication

Authentication plugin for CakePHP

1153.6M67](/packages/cakephp-authentication)[cakephp/authorization

Authorization abstraction layer plugin for CakePHP

742.2M34](/packages/cakephp-authorization)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[yiisoft/yii-middleware

Yii Middleware

21151.3k1](/packages/yiisoft-yii-middleware)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
