PHPackages                             binsoul/net-http-dispatcher - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. binsoul/net-http-dispatcher

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

binsoul/net-http-dispatcher
===========================

Dispatcher using named parameters to invoke callables or methods of objects

023PHP

Since Apr 27Pushed 10y ago1 watchersCompare

[ Source](https://github.com/binsoul/net-http-dispatcher)[ Packagist](https://packagist.org/packages/binsoul/net-http-dispatcher)[ RSS](/packages/binsoul-net-http-dispatcher/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

net-http-dispatcher
===================

[](#net-http-dispatcher)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f9fc0d8348953065555f3816da3a3d4a4adb3b9d35741eb808b25c7f1beea03f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62696e736f756c2f6e65742d687474702d646973706174636865722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/binsoul/net-http-dispatcher)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/ac850fba6d80f777ea23f26691d018fae9a3c8c4620a23359ec7cc7bbf0225ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62696e736f756c2f6e65742d687474702d646973706174636865722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/binsoul/net-http-dispatcher)

This package allows to map arbitrary names to dispatchable objects using named parameters provided in a context array. Dispatchable objects are then invoked to return a response. They can be surrounded by middleware to alter the incoming request or the outgoing response.

Install
-------

[](#install)

Via composer:

```
$ composer require binsoul/net-http-dispatcher
```

Responder
---------

[](#responder)

A responder can be a closure, an object with an \_\_invoke method or an object with multiple methods. It can receive the request object, the context object or any value from the context as parameters and it has to return a response.

The dispatcher resolves parameters via reflection. If a parameter cannot be resolved and is not optional an exception is thrown.

For example the responder "blog.edit" will receive the following parameters:

```
$dispatcher->addResponder(
    'blog.edit',
    function ($id, RequestInterface $request, Context $context, $optional = 'foo')
    {
        // $id = 1
        // $request = object
        // $context = object
        // $optional = 'foo'

        return new Response(...);
    }
);

$dispatcher->handle($request, ['responder' => 'blog.edit', 'id' => 1]);
```

Middleware
----------

[](#middleware)

Middleware can be a closure or an object with an \_\_invoke method. It has to accept a request, a context and the next middleware as parameters and it has to return a response.

For example middleware may or may not perform the various optional processes:

```
$middleware = function (RequestInterface $request, Context $context, callable $next) {
    // optionally return a response early
    if (...) {
        return new Response(...);
    }

    // optionally modify the request
    $request = $request->withUri(..);

    // optionally modify the context
    $context = $context->withParameter(...);

    // invoke the $next middleware
    $response = $next($request, $context);

    // optionally modify the response
    $response = $response->withStatus(...);

    // always return a response
    return $response;
};
```

Dispatcher
----------

[](#dispatcher)

A dispatcher uses the information provided by a context array to find a responder. The default keys are "responder" to indicate which responder should be invoked and "method" to indicate which method of the responder should be called.

The used array keys can be configured. The following example maps the reponder to the array key "controller" and the method to "action":

```
class BlogController
{
    public function index()
    {
        return new Response(...);
    }
}

$dispatcher->addResponder('blog', new BlogController());

$dispatcher->defineParameters('controller', 'action');

// will invoke BlogController->index()
$dispatcher->handle($request, ['controller' => 'blog', 'action' => 'index']);
```

Responders can be registered either as closures or concrete objects or as strings. If a responder is registered as a string the provided factory is used to lazily build responders.

```
$dispatcher->addResponder('blog', 'BlogResponder');
$dispatcher->setFactory($factory);

// will call $factory->buildResponder('BlogResponder');
$dispatcher->handle($request, ['responder' => 'blog']);
```

The same applies to registering middlewares.

```
$dispatcher->addMiddleware('AuthMiddleware')
$dispatcher->setFactory($factory);

// will call $factory->buildMiddleware('AuthMiddleware');
$dispatcher->handle($request, ['responder' => 'blog']);
```

Middleware is added to an internal queue which surrounds the final call to the responder.

For example if the queue is build like:

```
$dispatcher->addResponder('blog', 'BlogResponder');

$dispatcher->addMiddleware('Foo');
$dispatcher->addMiddleware('Bar');
$dispatcher->addMiddleware('Baz');

$dispatcher->handle($request, ['controller' => 'blog']);
```

The request and response path through the middlewares will look like this:

```
Foo is 1st on the way in
    Bar is 2nd on the way in
        Baz is 3rd on the way in
            BlogResponder is invoked
        Baz is 1st on the way out
    Bar is 2nd on the way out
Foo is 3rd on the way out

```

Testing
-------

[](#testing)

```
$ composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

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

---

Top Contributors

[![binsoul](https://avatars.githubusercontent.com/u/14879936?v=4)](https://github.com/binsoul "binsoul (5 commits)")

### Embed Badge

![Health badge](/badges/binsoul-net-http-dispatcher/health.svg)

```
[![Health](https://phpackages.com/badges/binsoul-net-http-dispatcher/health.svg)](https://phpackages.com/packages/binsoul-net-http-dispatcher)
```

###  Alternatives

[tiime/en-16931

EN-16931 compliant invoices as PHP objects

13239.4k3](/packages/tiime-en-16931)

PHPackages © 2026

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