PHPackages                             josantonius/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. [Framework](/categories/framework)
4. /
5. josantonius/router

AbandonedArchivedLibrary[Framework](/categories/framework)

josantonius/router
==================

Library for handling routes.

1.1.2(3y ago)246323MITPHPPHP ^5.6 || ^7.0

Since Mar 16Pushed 3y ago1 watchersCompare

[ Source](https://github.com/josantonius/php-router)[ Packagist](https://packagist.org/packages/josantonius/router)[ GitHub Sponsors](https://github.com/Josantonius)[ RSS](/packages/josantonius-router/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (10)Dependencies (5)Versions (14)Used By (3)

PHP Router library
==================

[](#php-router-library)

[![Latest Stable Version](https://camo.githubusercontent.com/0b3d330ae2d839dd4e66549fc5f3798ce01e6f1ac798d530999043b2d3bf1ff1/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f526f757465722f762f737461626c65)](https://packagist.org/packages/josantonius/Router)[![License](https://camo.githubusercontent.com/aa514185ba2d8ceaeeb48b0d2d35f295ca250ed2be9cb34e6315b7c4e5f20ef0/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f526f757465722f6c6963656e7365)](LICENSE)

[Versión en español](README-ES.md)

Library for handling routes.

> This documentation refers to version [1.1.1](https://github.com/josantonius/php-router/tree/1.1.1). Changes made in version 1.1.2 (which archived the repository) were not documented or tested.

---

- [Requirements](#requirements)
- [Installation](#installation)
- [Available Methods](#available-methods)
- [Quick Start](#quick-start)
- [Usage](#usage)
- [Tests](#tests)
- [Sponsor](#Sponsor)
- [License](#license)

---

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

[](#requirements)

This library is supported by **PHP versions 5.6** or higher and is compatible with **HHVM versions 3.0** or higher.

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

[](#installation)

The preferred way to install this extension is through [Composer](http://getcomposer.org/download/).

To install **PHP Router library**, simply:

```
composer require Josantonius/Router

```

The previous command will only install the necessary files, if you prefer to **download the entire source code** you can use:

```
composer require Josantonius/Router --prefer-source

```

You can also **clone the complete repository** with Git:

$ git clone

Or **install it manually**:

Download [Router.php](https://raw.githubusercontent.com/Josantonius/PHP-Router/master/src/Router.php) and [Url.php](https://raw.githubusercontent.com/Josantonius/PHP-Url/master/src/Url.php):

```
wget https://raw.githubusercontent.com/Josantonius/PHP-Router/master/src/Router.php

wget https://raw.githubusercontent.com/Josantonius/PHP-Url/master/src/Url.php

```

Available Methods
-----------------

[](#available-methods)

Available methods in this library:

### - Set method name for use singleton pattern

[](#--set-method-name-for-use-singleton-pattern)

```
Router::setSingletonName($method);
```

AttributeDescriptionTypeRequiredDefault$methodSingleton method name.stringYes**\# Return** (boolean)

### - Add route/s

[](#--add-routes)

```
Router::add($routes);
```

AttributeKeyDescriptionTypeRequiredDefault$routesRoute/s to add.arrayYes0Route.stringYes1Method 'class@method'.stringYes**\# Return** (boolean)

### - Get method to call from URI

[](#--get-method-to-call-from-uri)

```
Router::getMethod($route);
```

AttributeDescriptionTypeRequiredDefault$routeRoute.stringYes**\# Return** (string|null) → route or null

### - Defines callback if route is not found

[](#--defines-callback-if-route-is-not-found)

```
Router::error($callback);
```

AttributeDescriptionTypeRequiredDefault$callbackCallback.callableYes**\# Return** (boolean true)

### - Continue processing after match or stop it

[](#--continue-processing-after-match-or-stop-it)

Also can specify the number of total routes to process.

```
Router::keepLooking($value);
```

AttributeDescriptionTypeRequiredDefault$valueValue.booleanintYes**\# Return** (boolean true)

### - Runs the callback for the given request

[](#--runs-the-callback-for-the-given-request)

```
Router::dispatch();
```

**\# Return** (call response|false)

Quick Start
-----------

[](#quick-start)

To use this library with **Composer**:

```
require __DIR__ . '/vendor/autoload.php';

use Josantonius\Router\Router;
```

Or If you installed it **manually**, use it:

```
require_once __DIR__ . '/Router.php';
require_once __DIR__ . '/Url.php';

use Josantonius\Router\Router;
```

Usage
-----

[](#usage)

[Example](tests/Example.php) of use for this library:

### - Add route

[](#--add-route)

```
Router::add([
    'services' => 'Josantonius\Router\Example@services'
]);
```

### - Add routes

[](#--add-routes-1)

```
$routes = [
    'services' => 'Josantonius\Router\Example@services',
    'home'     => 'Josantonius\Router\Example@home',
];

Router::add($routes);
```

### - Execute route simulating 'services'

[](#--execute-route-simulating-services)

```
Router::dispatch(); // Response from services method
```

### - Add route with regular expressions (:all)

[](#--add-route-with-regular-expressions-all)

```
Router::add([
    'blog/:all' => 'Josantonius\Router\Example@blog'
]);
```

### - Execute route simulating 'language/PHP/'

[](#--execute-route-simulating-languagephp)

```
Router::dispatch(); // Response from services method
```

### - Add route with regular expressions (:any) and params

[](#--add-route-with-regular-expressions-any-and-params)

```
Router::add([
    'blog/:any/:any/' => 'Josantonius\Router\Example@blog',
]);
```

### - Execute route simulating 'blog/games/Minecraft/'

[](#--execute-route-simulating-bloggamesminecraft)

```
Router::dispatch(); // Response from blog method: games | Minecraft
```

### - Add route with regular expressions (:num) and params

[](#--add-route-with-regular-expressions-num-and-params)

```
Router::add([
    blog/:any/:num/' => 'Josantonius\Router\Example@blog',
]);
```

### - Execute route simulating 'blog/development/1/'

[](#--execute-route-simulating-blogdevelopment1)

```
Router::dispatch(); // Response from blog method: development | 1
```

### - Add route with regular expressions (:hex) and params

[](#--add-route-with-regular-expressions-hex-and-params)

```
Router::add([
    'blog/:any/:hex/' => 'Josantonius\Router\Example@blog',
]);
```

### - Execute route simulating 'blog/color/e0a060/'

[](#--execute-route-simulating-blogcolore0a060)

```
Router::dispatch(); // Response from blog method: color | e0a060
```

### - Add route with regular expressions (:uuidV4) and params

[](#--add-route-with-regular-expressions-uuidv4-and-params)

```
Router::add([
    'blog/:any/:uuidV4/' => 'Josantonius\Router\Example@blog',
]);
```

### - Execute route simulating 'blog/uuid/11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000/'

[](#--execute-route-simulating-bloguuid11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000)

```
Router::dispatch(); // Response from blog method: uuid | 11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000
```

### - Set method name for use singleton pattern

[](#--set-method-name-for-use-singleton-pattern-1)

```
Router::setSingletonName('newSingleton');
```

### - Get method

[](#--get-method)

```
Router::getMethod('services'); // Josantonius\Router\Example@services
```

### - Defines callback if route is not found

[](#--defines-callback-if-route-is-not-found-1)

```
Router::error('Josantonius\Router\Example@error');
```

### - Execute wrong routes with custom error callback

[](#--execute-wrong-routes-with-custom-error-callback)

```
Router::dispatch('unknown'); // Response from error method
```

### - Continue processing after match

[](#--continue-processing-after-match)

```
Router::keepLooking();
```

### - Keep Lookin up to three coincidences

[](#--keep-lookin-up-to-three-coincidences)

```
Router::keepLooking(3);
```

### - Stopping processing after match

[](#--stopping-processing-after-match)

```
Router::keepLooking(false);
```

Tests
-----

[](#tests)

To run [tests](tests) you just need [composer](http://getcomposer.org/download/) and to execute the following:

```
git clone https://github.com/Josantonius/PHP-Router.git

cd PHP-Router

composer install

```

Run unit tests with [PHPUnit](https://phpunit.de/):

```
composer phpunit

```

Run [PSR2](http://www.php-fig.org/psr/psr-2/) code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):

```
composer phpcs

```

Run [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:

```
composer phpmd

```

Run all previous tests:

```
composer tests

```

Sponsor
-------

[](#sponsor)

If this project helps you to reduce your development time, [you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work 😊

License
-------

[](#license)

This repository is licensed under the [MIT License](LICENSE).

Copyright © 2016-2022, [Josantonius](https://github.com/josantonius#contact)

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~165 days

Recently: every ~435 days

Total

13

Last Release

1412d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.6

1.0.2PHP ^5.6 || ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b221283501ec8a9cbaefaf27821a91ae8ddd33bddf1fccc6c6815b7ad216ff1?d=identicon)[Josantonius](/maintainers/Josantonius)

---

Top Contributors

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

---

Tags

phpphp-routerphproutesrouter

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

PHPackages © 2026

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