PHPackages                             makise-co/http-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. [HTTP &amp; Networking](/categories/http)
4. /
5. makise-co/http-router

ActiveLibrary[HTTP &amp; Networking](/categories/http)

makise-co/http-router
=====================

PSR HTTP Router

v1.0.4(5y ago)35171MITPHPPHP &gt;=7.4

Since Oct 15Pushed 5y ago1 watchersCompare

[ Source](https://github.com/makise-co/http-router)[ Packagist](https://packagist.org/packages/makise-co/http-router)[ RSS](/packages/makise-co-http-router/feed)WikiDiscussions main Synced today

READMEChangelog (5)Dependencies (9)Versions (6)Used By (1)

PSR HTTP Router
===============

[](#psr-http-router)

A PSR HTTP Router based on FastRoute with Middleware support

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

[](#requirements)

- PHP &gt;= 7.4

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

[](#installation)

`composer require makise-co/http-router`

Features
--------

[](#features)

- Middlewares
- Dependency Injection to Route Handlers (through PSR Container and [php-di/invoker](https://github.com/PHP-DI/Invoker))
- Supports all of most common request handler declarations (Closures, callables, array-callables, class@method, class::method)
- Strict PSR standards following (PSR-7 and PSR-15), each route handler **MUST** return instance of PSR ResponseInterface
- High portability and customization (any implementation part can be replaced with your own implementation)

FAQ
---

[](#faq)

- How to get the Route instance that is processing the request?

    ```
    $route = $request->getAttribute(RouteInterface::class);
    ```
- How to get route arguments?

    ```
    // method 1 - Using DependencyInjection, many frameworks provides this way
    function apiEndpoint(int $id): \Psr\Http\Message\ResponseInterface
    {
    }

    // method 2 - Manually getting route arguments
    function apiEndpoint(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface
    {
        $args = $request->getAttribute(Router::ROUTE_ARGS);
    }
    ```

Performance
-----------

[](#performance)

For three million calls (using php-di/invoker):

```
Time took: 4.95980096 secs (0.00000165 secs per request)

```

For three million calls (using native function call implementation without DI):

```
Time took: 3.31491995 secs (0.00000110 secs per request)

```

Benchmark code can be found [here](examples/benchmark.php).

- Benchmarks were performed on PHP 7.4 with OPcache enabled
- CPU: Intel Core i7-9750H 6 cores (CPU frequency during benchmarks: 4.07 GHz)
- OS: Ubuntu 20.04 (WSL 2)

Usage
-----

[](#usage)

```
