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

ActiveLibrary[Framework](/categories/framework)

anax/router
===========

Anax Router module, route requests to handlers.

v2.1.0(6y ago)018.4k↓90%119MITPHPPHP &gt;=7.2CI failing

Since Mar 7Pushed 6y ago1 watchersCompare

[ Source](https://github.com/canax/router)[ Packagist](https://packagist.org/packages/anax/router)[ Docs](https://dbwebb.se/anax)[ RSS](/packages/anax-router/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (5)Versions (44)Used By (19)

Anax Router
===========

[](#anax-router)

[![Latest Stable Version](https://camo.githubusercontent.com/e3200d6a96b1ec2dc926013916415b369941cf113bb6ce903ee1480fb306e51f/68747470733a2f2f706f7365722e707567782e6f72672f616e61782f726f757465722f762f737461626c65)](https://packagist.org/packages/anax/router)[![Join the chat at https://gitter.im/canax/router](https://camo.githubusercontent.com/4af6d19e2cb9517d6d8b57298d3f329e9711d3564979d4d428a40213d665a37e/68747470733a2f2f6261646765732e6769747465722e696d2f63616e61782f726f757465722e737667)](https://gitter.im/canax/router?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

[![Build Status](https://camo.githubusercontent.com/749a11391495e52325404949bbfd69e158c8404bd869d38e08bdc41cee1584d1/68747470733a2f2f7472617669732d63692e6f72672f63616e61782f726f757465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/canax/router)[![CircleCI](https://camo.githubusercontent.com/29726ac590605bb367132396fd97a01a736395ef4ba70d90b66ba85560e9a622/68747470733a2f2f636972636c6563692e636f6d2f67682f63616e61782f726f757465722e7376673f7374796c653d737667)](https://circleci.com/gh/canax/router)

[![Build Status](https://camo.githubusercontent.com/c049e68101efc9225906a86cfc1d8ab82a1db8aff965a9e1bdd62ede30b1b12f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f726f757465722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/router/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e524b6fbc195a1ca60e39ba0a8823c7aec5336fcaf3b57e8bb1f34061af0ce84/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f726f757465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/router/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/7992414383fc329d34274c32b6ff6bfbedfe3f903fe13f918779cf981331efac/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f726f757465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/router/?branch=master)

[![Maintainability](https://camo.githubusercontent.com/a58c1d28c4f59eb662bad088d19181defdb7309b19667b1b370e23da884e644d/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f30333332326661373836346164323461396232612f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/canax/router/maintainability)[![Codacy Badge](https://camo.githubusercontent.com/fc39cf708d50ac1b93f94fc44f56ef867351f4d061b1e311d10b0754dd783201/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3033393530616563383536353461656562343433653963366362393732643163)](https://www.codacy.com/app/mosbth/router?utm_source=github.com&utm_medium=referral&utm_content=canax/router&utm_campaign=Badge_Grade)

Anax Router module.

A standalone router supporting request methods and dynamic routes matching, extracting and validating arguments from path.

The router will try matching routes by the order they were added and execute all matching routes, one after the other.

Use `exit()` to prevent further routes from being matched.

Install
-------

[](#install)

```
$ composer require anax/router
```

Usage
-----

[](#usage)

### Add some routes with handlers

[](#add-some-routes-with-handlers)

```
use Anax\Route\Router;

$router = new Router();

$router->add("", function () {
    echo "home ";
});

$router->add("about", function () {
    echo "about ";
});

$router->add("about/me", function () {
    echo "about/me ";
});

// try it out
$router->handle("");
$router->handle("about");
$router->handle("about/me");
// home about about/me
```

### Add multiple routes with one handler

[](#add-multiple-routes-with-one-handler)

Add multiple routes, through an array of rules, sharing a handler.

```
$router = new Router();

$router->add(["info", "about"], function () {
    echo "info or about - ";
});

// try it out
$router->handle("info");
$router->handle("about");
// info or about - info or about -
```

### Add a default route

[](#add-a-default-route)

This route will match any path.

```
$router = new Router();

$router->always(function () {
    echo "always ";
});

// try it out using some paths
$router->handle("info");
$router->handle("about");
// always always
```

### Add internal routes for 404, 403 and 500 error handling

[](#add-internal-routes-for-404-403-and-500-error-handling)

Add an internal route that is called when no route can be matched.

```
$router = new Router();

$router->addInternal("404", function () {
    echo "404 ";
});

$router->add("about", function () {
    echo "about ";
});

// try it out using some paths
$router->handle("whatever");
// 404
```

You can add internal routes to deal with 403 and 500. These routes will handle uncaught exceptions thrown within a route handler.

The 403 internal route that is catching exception of type `ForbiddenException`.

```
$router->addInternal("403", function () {
    echo "403 ";
});

$router->add("login", function () {
    throw new ForbiddenException();
});

// try it out using some paths
$router->handle("login");
// 403
```

The 500 internal route that is catching exception of type `InternalErrorException`.

```
$router->addInternal("500", function () {
    echo "500 ";
});

$router->add("calculate", function () {
    throw new InternalErrorException();
});

// try it out using some paths
$router->handle("calculate");
// 500
```

### Add a common route for any item below subpath using \*

[](#add-a-common-route-for-any-item-below-subpath-using-)

This route will match any item on the same level as `about/*`.

```
$router = new Router();

$router->addInternal("404", function () {
    echo "404 ";
});

$router->add("about/*", function () {
    echo "about ";
});

// try it out using some paths
$router->handle("about");
$router->handle("about/me");
$router->handle("about/you");
$router->handle("about/some/other"); // no match
// about about about 404
```

### Add a common route for any item below subpath using \*\*

[](#add-a-common-route-for-any-item-below-subpath-using--1)

This route will match any item below `about/**`, even subdirs.

```
$router = new Router();

$router->add("about/**", function () {
    echo "about ";
});

// try it out using some paths
$router->handle("about");
$router->handle("about/me");
$router->handle("about/you");
$router->handle("about/some/other");
// about about about about
```

### Part of path as arguments to the route handler

[](#part-of-path-as-arguments-to-the-route-handler)

You can send a part of the route as an argument to the handler. This makes a route handler more flexible and dynamic.

```
$router = new Router();

$router->addInternal("404", function () {
    echo "404 ";
});

$router->add("about/{arg}", function ($arg) {
    echo "$arg ";
});

ob_start();
// try it out using some paths
$router->handle("about");            // not matched
$router->handle("about/me");
$router->handle("about/you");
$router->handle("about/some/other"); // not matched
// 404 me you 404
```

You can send multiple arguments.

```
$router = new Router();

$router->add(
    "post/{year}/{month}/{day}",
    function ($year, $month, $day) {
        echo "$year-$month-$day, ";
    }
);

// try it out using some paths
$router->handle("post/2017/03/07");
$router->handle("post/1990/06/20");
// 2017-03-07, 1990-06-20,
```

### Type checking of arguments

[](#type-checking-of-arguments)

Apply type checking to the arguments to restrict a the routes being matched.

```
$router = new Router();

$router->addInternal("404", function () {
    echo "404, ";
});

$router->add(
    "post/{year:digit}/{month:digit}/{day:digit}",
    function ($year, $month, $day) {
        echo "$year-$month-$day, ";
    }
);

$router->add(
    "post/{year:digit}/{month:alpha}/{day:digit}",
    function ($year, $month, $day) {
        echo "$day $month $year, ";
    }
);

// try it out using some paths
$router->handle("post/2017/03/seven");
$router->handle("post/2017/03/07");
$router->handle("post/1990/06/20");
$router->handle("post/1990/june/20");
// 404, 2017-03-07, 1990-06-20, 20 june 1990,
```

For type checking is digit, alpha, alphanum and hex supported (see [ctype](http://php.net/manual/en/ref.ctype.php) for details).

### Routes per request method

[](#routes-per-request-method)

A route can be setup to match only one request method.

```
$router = new Router();

$router->any(["GET"], "about", function () {
    echo "GET ";
});

$router->any(["POST"], "about", function () {
    echo "POST ";
});

$router->any(["PUT"], "about", function () {
    echo "PUT ";
});

$router->any(["DELETE"], "about", function () {
    echo "DELETE ";
});

// try it out using some paths
$router->handle("about", "GET");
$router->handle("about", "POST");
$router->handle("about", "PUT");
$router->handle("about", "DELETE");
// GET POST PUT DELETE
```

A route can also match several request methods.

```
$router = new Router();

$router->any(["GET", "POST"], "about", function () {
    echo "GET+POST ";
});

$router->any("PUT | DELETE", "about", function () {
    echo "PUT+DELETE ";
});

// try it out using some paths
$router->handle("about", "GET");
$router->handle("about", "POST");
$router->handle("about", "PUT");
$router->handle("about", "DELETE");
// GET+POST GET+POST PUT+DELETE PUT+DELETE
```

Dependency
----------

[](#dependency)

These are the dependencies to other modules.

ModuleWhat`anax/commons`Using Anax\\Commons\\ContainerInjectableInterface.`anax/commons`Using Anax\\Commons\\ContainerInjectableTrait.License
-------

[](#license)

This software carries a MIT license. See [LICENSE.txt](LICENSE.txt) for details.

```
 .
..:  Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se

```

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 99.3% 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 ~28 days

Recently: every ~135 days

Total

43

Last Release

2226d ago

Major Versions

v1.1.0 → v2.0.0-alpha.12018-08-01

PHP version history (3 changes)v1.0.0PHP &gt;=5.6

v1.1.0PHP &gt;=7.0

v2.0.0-alpha.2PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/4cac244120f54c02a4fb3186b91323f3f0805dd9daefd0babeb4a82a19232faf?d=identicon)[mikael\_roos](/maintainers/mikael_roos)

---

Top Contributors

[![mosbth](https://avatars.githubusercontent.com/u/169550?v=4)](https://github.com/mosbth "mosbth (133 commits)")[![gitter-badger](https://avatars.githubusercontent.com/u/8518239?v=4)](https://github.com/gitter-badger "gitter-badger (1 commits)")

---

Tags

frameworkmicromvcboilerplateeducation

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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