PHPackages                             vivace/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. vivace/router

ActiveLibrary

vivace/router
=============

Simple http tree router

11PHP

Since May 21Pushed 8y agoCompare

[ Source](https://github.com/php-vivace/router)[ Packagist](https://packagist.org/packages/vivace/router)[ RSS](/packages/vivace-router/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Synopsis
--------

[](#synopsis)

A simple and fast router, nothing more. The tree-building mechanism is used to quickly find the route.

Code Example
------------

[](#code-example)

```
$switcher = new \vivace\router\Switcher([
    //To check the URL path in a template, the rule must begin with a slash, for example /foo/bar
    '/hello/:message' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        return 'Hello ' . $request->getAttribute('message');
    },
    '/' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        return 'Index Page';
    },
    'POST/user' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        return 'Create user';
    },
    'GET//:app.mysite.com/user/:id ' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        $app = $request->getAttribute('app');
        $id = $request->getAttribute('id');
        return "Request to $app for getting user by id=$id";
    },
    //domain rules should started with double slashes `//`
    '//admin.mysite.com' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        return 'Admin page';
    },
    //It supports nested in one another
    '/api' => new \vivace\router\Switcher([
        '/v1' => function () {
            return 'Nested switcher';
        },
        '/v2' => new \vivace\router\Switcher([
            '/' => function () {
                return 'Deep nested switcher';
            }
        ])
    ]),
    'OPTIONS' => function(){
        return 'Request with OPTIONS method.';
    }
]);
```

```
//$request and $response is objects, which implementation of psr-7, this library not contain implementation psr-7, you should use third-party libraries, which do it.
$response = $switcher->switch($request, $response);
echo $response->getBody()->getContents();
```

Motivation
----------

[](#motivation)

The goal was to create a fast router with support for routing over the host, capturing variables, embedding. Compatibility with PSR-7 was also required.

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

[](#installation)

`php composer.phar require vivace/router`

API Reference
-------------

[](#api-reference)

### Switcher::case(string $pattern, callable $handler)

[](#switchercasestring-pattern-callable-handler)

Add handler, which will called if request matched by pattern successfully.

### Switcher::switch(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface

[](#switcherswitchserverrequestinterface-request-responseinterface-response-responseinterface)

Find and call handler for request

### Switcher::default(callable $handler)

[](#switcherdefaultcallable-handler)

Add default handler, for case when route not matched.

Tests
-----

[](#tests)

##### Via local installed php

[](#via-local-installed-php)

phpunit --testsuite=unit --coverage-html=./tests/coverage/

##### Via docker-compose

[](#via-docker-compose)

docker-compose run --rm phpunit --testsuite=unit --coverage-html=./tests/coverage/

Contributors
------------

[](#contributors)

Albert Sultanov

License
-------

[](#license)

Copyright (c) 2017 Albert Sultanov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community6

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/ec6a48688cd2c91be77c3ba006a932445a93e5c8466557a2ba0c54f53b7508fc?d=identicon)[KPEMATOP](/maintainers/KPEMATOP)

---

Top Contributors

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

---

Tags

domainphpphp7psr7router

### Embed Badge

![Health badge](/badges/vivace-router/health.svg)

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

PHPackages © 2026

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