PHPackages                             yeremi/route-mapper - 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. [API Development](/categories/api)
4. /
5. yeremi/route-mapper

ActiveLibrary[API Development](/categories/api)

yeremi/route-mapper
===================

A PHP library for mapping and resolving routes using attributes.

v1.0.0(1y ago)031MITPHPCI passing

Since Jan 1Pushed 3mo ago2 watchersCompare

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

READMEChangelog (1)Dependencies (3)Versions (4)Used By (0)

RouteMapper
===========

[](#routemapper)

[![CI](https://github.com/yeremi/route-mapper/actions/workflows/php-qa.yml/badge.svg?branch=main)](https://github.com/yeremi/route-mapper/actions/workflows/php-qa.yml/badge.svg?branch=main)[![PHP Version](https://camo.githubusercontent.com/c43a736f738fa982a023ed7fc827895810d2ca0f353aa53674e0691bccea1034/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e302d626c7565)](https://camo.githubusercontent.com/c43a736f738fa982a023ed7fc827895810d2ca0f353aa53674e0691bccea1034/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e302d626c7565)[![License](https://camo.githubusercontent.com/8a9e2419ea1925791f6bf0dee3204d27e7f9fe3f51456554fc2db27a5ca44677/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f796572656d692f726f7574652d6d6170706572)](https://camo.githubusercontent.com/8a9e2419ea1925791f6bf0dee3204d27e7f9fe3f51456554fc2db27a5ca44677/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f796572656d692f726f7574652d6d6170706572)[![Latest Version](https://camo.githubusercontent.com/929d0fa473959b75de60d0abdd5cffdb4079b731ec8fa1cb804353ea3419ec85/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796572656d692f726f7574652d6d6170706572)](https://camo.githubusercontent.com/929d0fa473959b75de60d0abdd5cffdb4079b731ec8fa1cb804353ea3419ec85/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796572656d692f726f7574652d6d6170706572)[![Downloads](https://camo.githubusercontent.com/a9512018717c915d5a4a46b4199dca88bb0075115f1d027a2e2366d293182617/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796572656d692f726f7574652d6d6170706572)](https://camo.githubusercontent.com/a9512018717c915d5a4a46b4199dca88bb0075115f1d027a2e2366d293182617/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796572656d692f726f7574652d6d6170706572)

**RouteMapper** is a lightweight, framework-agnostic PHP library that uses **PHP 8 attributes** to map and resolve API route paths defined via PHP attributes in a simple and explicit way.

It focuses on one responsibility only: **mapping routes to methods**, without imposing an HTTP framework or execution model.

---

Design Philosophy
-----------------

[](#design-philosophy)

- RouteMapper focuses on **one responsibility only**: mapping routes to methods.
- It does **not** handle HTTP requests, middleware, controllers, or responses.
- Integration is explicit by design — no magic containers or hidden behavior.
- The library favors **clarity over convenience**.
- If you need a full routing framework, RouteMapper is probably not the right tool.

---

Key Features
------------

[](#key-features)

- **Attribute-Based Routing** using PHP 8 attributes
- **Framework-Agnostic** (works with any HTTP client or framework)
- **Lightweight &amp; Focused** — no unnecessary abstractions
- **Explicit Route Resolution** — no magic, full control
- **Modern PHP** — strict typing and PHP 8 features

---

Requirements
------------

[](#requirements)

- PHP 8.0 or higher

---

Why Use RouteMapper?
--------------------

[](#why-use-routemapper)

Use RouteMapper when you want to keep routing metadata close to your code (via attributes) and still remain framework-agnostic. It works well for SDKs, API clients, small services, and projects that want explicit, testable route resolution without adopting a full stack.

---

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

[](#installation)

Install RouteMapper via Composer:

```
composer require yeremi/route-mapper
```

---

Example Usage
-------------

[](#example-usage)

> Routes are resolved using the class name and method name.

### Defining routes with attributes

[](#defining-routes-with-attributes)

```
use Yeremi\RouteMapper\Attribute\ApiRoute;
use Yeremi\RouteMapper\Registry\RouteRegistry;
use Yeremi\RouteMapper\Resolver\RouteResolver;

// (for this example): `composer require guzzlehttp/guzzle`
use GuzzleHttp\Client; // Example only

class UserRepository
{
    public function __construct(
        protected RouteRegistry $routeRegistry,
        protected RouteResolver $routeResolver,
        protected Client $httpClient
    ) {
        // Registers all #[ApiRoute] attributes in this class
        $this->routeRegistry->registerRoutes($this);
    }

    #[ApiRoute('/users')] // Defines only the route path
    public function fetchAll(): void
    {
        $route = $this->resolveRoute(__FUNCTION__);
        $response = $this->httpClient->get($route);
        echo $response->getBody()->getContents();
    }

    #[ApiRoute('/user/{id}')]
    public function fetchOne(int $id): void
    {
        $route = $this->resolveRoute(__FUNCTION__, ['id' => $id]);
        $response = $this->httpClient->get($route);

        if ($response->getStatusCode() === 200) {
            $data = json_decode($response->getBody()->getContents(), true);
            // Process the $data as needed.
        }
    }

    #[ApiRoute('/user/create')]
    public function create(array $data): void
    {
        $route = $this->resolveRoute(__FUNCTION__);
        $response = $this->httpClient->post($route, [
            'json' => $data,
        ]);
        echo "User Created: " . $response->getBody()->getContents() . "\n";
    }

    #[ApiRoute('/user/{id}/update')]
    public function update(int $id, array $data): void
    {
        $parameters = ['id' => $id];
        $route = $this->resolveRoute(__FUNCTION__, $parameters);

        $response = $this->httpClient->put($route, [
            'json' => $data,
        ]);
        echo "User Updated: " . $response->getBody()->getContents() . "\n";
    }

    #[ApiRoute('/user/{id}/delete')]
    public function delete(int $id): void
    {
        $parameters = ['id' => $id];
        $route = $this->resolveRoute(__FUNCTION__, $parameters);

        $response = $this->httpClient->delete($route);
        echo "User Deleted: " . $response->getBody()->getContents() . "\n";
    }

    private function resolveRoute(string $methodName, array $parameters = []): string
    {
        $route = $this->routeRegistry->getRoute(static::class, $methodName);

        if (!$route) {
            throw new \RuntimeException("Route not found for method: $methodName");
        }

        return $this->routeResolver->resolve($route, $parameters);
    }
}

// Usage example:
$routeRegistry = new RouteRegistry();
$routeResolver = new RouteResolver();
$httpClient = new Client([
    'base_uri' => 'https://api.example.com',
    'timeout'  => 5.0,
]);

$userRepository = new UserRepository($routeRegistry, $routeResolver, $httpClient);
// Fetch one user
$userRepository->fetchOne(123);
// Fetch all users
$userRepository->fetchAll();
// Create a user
$userRepository->create(['name' => 'John Doe', 'email' => 'john@example.com']);
// Update a user
$userRepository->update(123, ['name' => 'John Doe Updated']);
// Delete a user
$userRepository->delete(123);
```

> Note: RouteMapper does not perform HTTP requests. Any HTTP client (Guzzle, Symfony HttpClient, etc.) can be used.

---

Comparison with Other Solutions
-------------------------------

[](#comparison-with-other-solutions)

FeatureRouteMapperSymfony RoutingLaravel RoutingSlim Framework**Attribute-based**✅✅❌❌**Framework-Agnostic**✅❌❌✅**Lightweight**✅⚠️❌✅**Explicit resolution**✅❌❌⚠️Acknowledgments
---------------

[](#acknowledgments)

Inspired by the flexibility of modern PHP attributes and the simplicity of middleware-based frameworks.

---

License
-------

[](#license)

RouteMapper is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance63

Regular maintenance activity

Popularity7

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% 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

Unknown

Total

1

Last Release

493d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6e70fd65b778a9e9dde98c0e377004ff4b8b06759c90cffb21425102627ad7df?d=identicon)[Yeremi](/maintainers/Yeremi)

---

Top Contributors

[![yeremi](https://avatars.githubusercontent.com/u/744184?v=4)](https://github.com/yeremi "yeremi (7 commits)")[![semantic-release-bot](https://avatars.githubusercontent.com/u/32174276?v=4)](https://github.com/semantic-release-bot "semantic-release-bot (1 commits)")

---

Tags

attributesmappingsphp8routenormalizerapiattributesroute

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/yeremi-route-mapper/health.svg)

```
[![Health](https://phpackages.com/badges/yeremi-route-mapper/health.svg)](https://phpackages.com/packages/yeremi-route-mapper)
```

###  Alternatives

[nezamy/route

Route - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.

21436.6k5](/packages/nezamy-route)[m165437/laravel-blueprint-docs

API Blueprint Renderer for Laravel

22779.0k](/packages/m165437-laravel-blueprint-docs)[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)
