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

ActiveWindwalker-package[Framework](/categories/framework)

windwalker/router
=================

Windwalker Router package

3.5.23(5y ago)13.6k1LGPL-2.0-or-laterPHPPHP &gt;=7.1.3

Since Oct 5Pushed 5y ago3 watchersCompare

[ Source](https://github.com/ventoviro/windwalker-router)[ Packagist](https://packagist.org/packages/windwalker/router)[ Docs](https://github.com/ventoviro/windwalker-router)[ RSS](/packages/windwalker-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (82)Used By (1)

Windwalker Router
=================

[](#windwalker-router)

Installation via Composer
-------------------------

[](#installation-via-composer)

Add this to the require block in your `composer.json`.

```
{
    "require": {
        "windwalker/router": "~3.0"
    }
}
```

Getting Started
---------------

[](#getting-started)

```
use Windwalker\Router\Router;

$router = new Router;
```

### Add Routes

[](#add-routes)

```
use Windwalker\Route\Route;

// Route with name
$router->addRoute(new Route('sakura', 'flower/(id)/sakura', array('_controller' => 'SakuraController')));

// Route without name
$router->addRoute(new Route(null, 'flower/(id)/sakura', array('_controller' => 'SakuraController')));
```

### Match Route

[](#match-route)

```
$route = $router->match('flower/12/sakura');

$variables = $route->getVariables(); // Array([_controller] => SakuraController)

// Use variables
$class = $variables['_controller'];

$controller = new $class;
```

### Add More Options to Route

[](#add-more-options-to-route)

Route interface: '($name, $pattern\[, $variables = array()\]\[, $allowMethods = array()\]\[, $options = array()\])'

```
$route = new Route(
    'name',
    'pattern/of/route/(id).(format)',

    // Default Variables
    array(
        'id'    => 1,
        'alias' => 'foo-bar-baz',
        'format' => 'html'
    ),

    // Allow methods
    array('GET', 'POST'),

    // Options
    array(
        'host'    => 'windwalker.io',
        'scheme'  => 'http', // Only http & https
        'port'    => 80,
        'sslPort' => 443,
        'requirements' => array(
            'id' => '\d+'
        ),
        'extra' => array(
            '_ctrl' => 'Controller\Class\Name',
        )
    )
);

$router->addRoute($route);

// match routes
$route = $router->match(
    'pattern/of/route/25.html',
    array(
        'host'   => $uri->getHost(),
        'scheme' => $uri->getScheme(),
        'port'   => $uri->getPort()
    )
);

$variables = $route->getVariables();

// Merge these matched variables back to http request
$_REQUEST = array_merge($_REQUEST, $variables);

// Extra is the optional variables but we won't want to merge into request
$extra = $router->getExtra();

print_r($variables);
print_r($extra);
```

The printed result:

```
Array
(
    [id] => 25
    [alias] => foo-bar-baz
    [format] => html
)

Array(
    [_ctrl] => Controller\Class\Name
)

```

### Build Route

[](#build-route)

`build()` is a method to generate route uri for view template.

```
$router->addRoute(new Route('sakura', 'flower/(id)/sakura', array('_controller' => 'SakuraController')));

$uri = $router->build('sakura', array('id' => 30)); // flower/30/sakura

echo 'Link';
```

### Quick Mapping

[](#quick-mapping)

`addMap()` is a simple method to quick add route without complex options.

```
$router->addMap('flower/(id)/sakura', array('_controller' => 'SakuraController', 'id' => 1));

$variables = $router->match('flower/30/sakura');
```

Rules
-----

[](#rules)

### Simple Params

[](#simple-params)

```
new Route(null, 'flower/(id)-(alias)');
```

### Optional Params

[](#optional-params)

#### Single Optional Params

[](#single-optional-params)

```
new Route(null, 'flower(/id)');
```

Matched route could be:

```
flower
flower/25

```

#### Multiple Optional Params

[](#multiple-optional-params)

```
new Route(null, 'flower(/year,month,day)');
```

Matched route could be:

```
flower
flower/2014
flower/2014/10
flower/2014/10/12

```

The matched variables will be

```
Array
(
    [year] => 2014
    [month] => 10
    [day] => 12
)

```

### Wildcards

[](#wildcards)

```
// Match 'king/john/troilus/and/cressida'
new Route(null, 'flower/(*tags)');
```

Matched:

```
Array
(
    [tags] => Array
    (
        [0] => john
        [1] => troilus
        [2] => and
        [3] => cressida
    )
)

```

Matchers
--------

[](#matchers)

Windwalker Router provides some matchers to use different way to match routes.

### Sequential Matcher

[](#sequential-matcher)

Sequential Matcher use the [Sequential Search Method](http://en.wikipedia.org/wiki/Linear_search) to find route. It is the slowest matcher but much more customizable. It is the default matcher of Windwalker Router.

```
use Windwalker\Router\Matcher\SequentialMatcher;

$router = new Router(array(), new SequentialMatcher);
```

### Binary Matcher

[](#binary-matcher)

Binary Matcher use the [Binary Search Algorithm](http://en.wikipedia.org/wiki/Binary_search_algorithm) to find route. This matcher is faster than SequentialMatcher but it will break the ordering of your routes. Binary search will re-sort all routes by pattern characters.

```
use Windwalker\Router\Matcher\BinaryMatcher;

$router = new Router(array(), new BinaryMatcher);
```

### Trie Matcher

[](#trie-matcher)

Trie Matcher use the [Trie](http://en.wikipedia.org/wiki/Trie) tree to search route. This matcher is the fastest method of Windwalker Router, but the limit is that it need to use an simpler route pattern which is not as flexible as the other two matchers.

```
use Windwalker\Router\Matcher\TrieMatcher;

$router = new Router(array(), new TrieMatcher);
```

### Rules of TrieMatcher

[](#rules-of-triematcher)

#### Simple Params

[](#simple-params-1)

only match when the uri segments all exists. If you want to use optional segments, you must add two or more patterns.

```
flower
flower/:id
flower/:id/:alias

```

#### Wildcards

[](#wildcards-1)

This pattern will convert segments after `flower/` this to an array which named `tags`:

```
flower/*tags

```

Single Action Router
--------------------

[](#single-action-router)

Single action router is a simple router that extends Windwalker Router. It just return a string if matched.

This is a single action controller example:

```
$router->addMap('flower/(id)/(alias)', 'FlowerController');

$controller = $router->match('flower/25/sakura');

$_REQUEST = array_merge($_REQUEST, $router->getVariables());

echo (new $controller)->execute();
```

Or a controller with action name:

```
$router->addMap('flower/(id)/(alias)', 'FlowerController::indexAction');

$matched = $router->match('flower/25/sakura');

$_REQUEST = array_merge($_REQUEST, $router->getVariables());

list($controller, $action) = explode('::', $matched);

echo (new $controller)->$action();
```

RestRouter
----------

[](#restrouter)

RestRouter is a simple router extends to SingleActionRouter, it can add some suffix of different methods.

```
$router->addMap('flower/(id)/(alias)', 'Flower\\Controller\\');

$controller = $router->match('flower/25/sakura', 'POST'); // Get Flower\\Controller\\Create

(new $controller)->execute();
```

Default Suffix mapping is:

```
'GET'     => 'Get',
'POST'    => 'Create',
'PUT'     => 'Update',
'PATCH'   => 'Update',
'DELETE'  => 'Delete',
'HEAD'    => 'Head',
'OPTIONS' => 'Options'

```

You can override it:

```
$router->setHttpMethodSuffix('POST', 'SaveController');
```

Exception
---------

[](#exception)

If Router not matched anything, it throws `Windwalker\Router\Exception\RouteNotFoundException`.

```
try
{
    $route = $router->match('flower/25');
}
catch (RouteNotFoundException $e)
{
    Application::close('Page not found', 404);

    exit();
}
catch (\Exception $e)
{
    // Do other actions...
}
```

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 98% 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 ~89 days

Total

80

Last Release

2035d ago

Major Versions

2.1.9 → 3.0-beta2016-07-03

PHP version history (3 changes)2.0.0-alphaPHP &gt;=5.3.10

3.2PHP &gt;=5.5.9

3.5PHP &gt;=7.1.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1639206?v=4)[Simon Asika](/maintainers/asika32764)[@asika32764](https://github.com/asika32764)

---

Top Contributors

[![asika32764](https://avatars.githubusercontent.com/u/1639206?v=4)](https://github.com/asika32764 "asika32764 (50 commits)")[![megamount](https://avatars.githubusercontent.com/u/17608213?v=4)](https://github.com/megamount "megamount (1 commits)")

---

Tags

phproutertrieframeworkrouterwindwalker

### Embed Badge

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

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

###  Alternatives

[slim/slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs

12.2k49.9M1.3k](/packages/slim-slim)[developermarius/simple-router

Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.

112.4k](/packages/developermarius-simple-router)

PHPackages © 2026

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