PHPackages                             jerowork/route-attribute-provider - 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. jerowork/route-attribute-provider

ActiveLibrary

jerowork/route-attribute-provider
=================================

Define routes by PHP8 attributes

0.8.0(1y ago)52.5k↓50%1[5 PRs](https://github.com/jerowork/route-attribute-provider/pulls)2MITPHPPHP ^8.1CI passing

Since Dec 7Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/jerowork/route-attribute-provider)[ Packagist](https://packagist.org/packages/jerowork/route-attribute-provider)[ RSS](/packages/jerowork-route-attribute-provider/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (9)Versions (17)Used By (2)

route-attribute-provider
========================

[](#route-attribute-provider)

[![Build Status](https://camo.githubusercontent.com/149370bb2ca922fca4f85cb0a8ab695b55b4ed6762f0a20fa1055cea359c9ab3/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e742e7376673f75726c3d6874747073253341253246253246616374696f6e732d62616467652e6174726f782e6465762532466a65726f776f726b253246726f7574652d6174747269627574652d70726f766964657225324662616467652533467265662533446d61696e267374796c653d666c61742d737175617265)](https://github.com/jerowork/route-attribute-provider/actions)[![Coverage Status](https://camo.githubusercontent.com/b866843cab0e355cf880b0e6c4ad5631b8cba5219abfef6229b092f16f18a29c/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6a65726f776f726b2f726f7574652d6174747269627574652d70726f76696465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/jerowork/route-attribute-provider/code-structure)[![Quality Score](https://camo.githubusercontent.com/0936c8fe44b8bf7b1a3c3a452c94ea1c6270255e600d3e3e31c1bfb43c51bb2d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6a65726f776f726b2f726f7574652d6174747269627574652d70726f76696465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/jerowork/route-attribute-provider)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/b69db4e1c2b7a95e001f8468147dd4dcb9a95993bd697425e845ad63730ada01/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a65726f776f726b2f726f7574652d6174747269627574652d70726f76696465722e7376673f7374796c653d666c61742d73717561726526696e636c7564655f70726572656c6561736573)](https://packagist.org/packages/jerowork/route-attribute-provider)[![PHP Version](https://camo.githubusercontent.com/3389172d235ce9321db25d1d256dfc51b1743da9b6d487179b04b93eedf7b05b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e312b2d3838393242462e7376673f7374796c653d666c61742d737175617265)](http://www.php.net)

Define routes by PHP8 attributes.

Installation
------------

[](#installation)

Install via [Composer](https://getcomposer.org):

```
$ composer require jerowork/route-attribute-provider
```

Configuration
-------------

[](#configuration)

In order to use route attributes, pick any of the [existing framework implementations](#existing-implementations) or create a custom one.

Instantiate `RouteAttributeConfigurator` somewhere close to the construction of your application, e.g. in your front controller (or ideally register in your PSR-11 container).

Basic configuration:

```
use Jerowork\RouteAttributeProvider\RouteAttributeConfigurator;

// ...

$routeConfigurator = new RouteAttributeConfigurator(
    new CustomRouteProvider($router)  // Implementation of your choice
);

$routeConfigurator
    ->addDirectory(sprintf('%s/src/Infrastructure/Api/Http/Action', __DIR__))
    ->configure();

// ...
```

Extended configuration:

```
use Jerowork\RouteAttributeProvider\RouteAttributeConfigurator;
use Jerowork\RouteAttributeProvider\RouteLoader\Roave\RegexIterator\RegexIteratorFileFinder;
use Jerowork\RouteAttributeProvider\RouteLoader\Roave\RoaveRouterLoader;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
// ...

// All parts of the configurator can be replaced with a custom implementation
$routeConfigurator = new RouteAttributeConfigurator(
    new CustomRouteProvider($router), // Implementation of your choice
    new RoaveRouterLoader(new RegexIteratorFileFinder()),
);

// Multiple directories can be defined
$routeConfigurator
    ->addDirectory(
        sprintf('%s/src/Infrastructure/Api/Http/Action', __DIR__),
        sprintf('%s/src/Other/Controller', __DIR__)
    )
    ->configure();

// ...
```

### Existing implementations

[](#existing-implementations)

- [jerowork/slim-route-attribute-provider](https://github.com/jerowork/slim-route-attribute-provider) for [Slim](https://www.slimframework.com)
- [brenoroosevelt/league-route-attribute-provider](https://github.com/brenoroosevelt/league-route-attribute-provider) for [League/Route](https://github.com/thephpleague/route)

Or check [packagist](https://packagist.org/providers/jerowork/route-attribute-provider-implementation) for any other implementations.

### Custom implementation

[](#custom-implementation)

Create a custom implementation by using `RouteAttributeProviderInterface`.

A (fictive) custom implementation:

```
use Jerowork\RouteAttributeProvider\Api\Route;
use Jerowork\RouteAttributeProvider\RouteAttributeProviderInterface;

final class CustomRouteProvider implements RouteAttributeProviderInterface
{
    private SomeRouter $router;

    public function __construct(SomeRouter $router)
    {
        $this->router = $router;
    }

    public function configure(string $className,string $methodName, Route $route) : void
    {
        // Register rule at your router
        $rule = $this->router->addRule(
            $route->getMethods(),
            $route->getPattern(),
            $className.':'.$methodName,
            $route->getName()
        );

        // Register optional middleware
        foreach ($route->getMiddleware() as $middleware) {
            $rule->addMiddleware($middleware);
        }
    }
}
```

### Cache

[](#cache)

By default, caching of route attributes is disabled. This is fine for a development environment.

However, for a production environment you should use a more efficient way of route attribute loading. Therefore you can use any [PSR-16](https://www.php-fig.org/psr/psr-16) cache implementation.

```
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Psr16Cache;

// ...

// Enable route attribute caching with any PSR-16 implementation (e.g. symfony/cache)
$routeConfigurator->enableCache(new Psr16Cache(new ApcuAdapter()));

// ...
```

**Note:** Any PSR-6 cache implementation can be used too, by using Symfony's [PSR-6 to PSR-16 adapter](https://symfony.com/doc/current/components/cache/psr6_psr16_adapters.html).

Usage
-----

[](#usage)

A route can be defined via PHP8 attributes.

Minimalist example:

```
use Jerowork\RouteAttributeProvider\Api\Route;
use Psr\Http\Message\ResponseInterface as ServerRequest;
use Psr\Http\Message\ServerRequestInterface as Response;

final class RootAction
{
    #[Route('/root')]
    public function __invoke(ServerRequest $request, Response $response) : Response
    {
        return $response;
    }
}
```

Extended example:

```
use Jerowork\RouteAttributeProvider\Api\RequestMethod;
use Jerowork\RouteAttributeProvider\Api\Route;
use Psr\Http\Message\ResponseInterface as ServerRequest;
use Psr\Http\Message\ServerRequestInterface as Response;

final class RootAction
{
    #[Route('/root', method: RequestMethod::GET, name: 'root', middleware: SomeMiddleware::class)]
    public function __invoke(ServerRequest $request, Response $response) : Response
    {
        return $response;
    }
}
```

Full-fledged example:

```
use Jerowork\RouteAttributeProvider\Api\RequestMethod;
use Jerowork\RouteAttributeProvider\Api\Route;
use Psr\Http\Message\ResponseInterface as ServerRequest;
use Psr\Http\Message\ServerRequestInterface as Response;

final class RootAction
{
    #[Route('/root',
        method: [RequestMethod::GET, RequestMethod::POST],
        name: 'root',
        middleware: [
            SomeMiddleware::class,
            AnotherMiddleware::class,
        ],
        host: 'localhost',
        schemes: ['http', 'https'],
        httpPort: 8888,
        httpsPort: 9999,
        options: ['strategy' => 'something']
    )]
    #[Route('/second-route',
        method: RequestMethod::DELETE,
        name: 'second-route'
    )]
    public function __invoke(ServerRequest $request, Response $response) : Response
    {
        return $response;
    }
}
```

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance66

Regular maintenance activity

Popularity26

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 75.6% 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 ~229 days

Recently: every ~340 days

Total

8

Last Release

379d ago

PHP version history (3 changes)0.1.0PHP ^8.0

0.6.0PHP ^8.0.2

0.7.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/f5fda898ea5700962c462ce21749b844fa13a9b239bfb52c95926a90d07c0ef5?d=identicon)[jerowork](/maintainers/jerowork)

---

Top Contributors

[![jerowork](https://avatars.githubusercontent.com/u/4119451?v=4)](https://github.com/jerowork "jerowork (65 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (17 commits)")[![angun33](https://avatars.githubusercontent.com/u/1091819?v=4)](https://github.com/angun33 "angun33 (4 commits)")

---

Tags

attributesphp8routerrouterattributesphp8

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jerowork-route-attribute-provider/health.svg)

```
[![Health](https://phpackages.com/badges/jerowork-route-attribute-provider/health.svg)](https://phpackages.com/packages/jerowork-route-attribute-provider)
```

###  Alternatives

[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

845.7k](/packages/tommyknocker-pdo-database-class)[uderline/openapi-php-attributes

Automatically render your OpenApi 3 file describing your PHP API using attributes

2136.3k](/packages/uderline-openapi-php-attributes)[shahghasiadil/laravel-api-versioning

Elegant attribute-based API versioning solution for Laravel applications with built-in deprecation management and version inheritance

2913.6k](/packages/shahghasiadil-laravel-api-versioning)

PHPackages © 2026

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