PHPackages                             kento-oka/roust - 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. kento-oka/roust

Abandoned → [fratily/router](/?search=fratily%2Frouter)Library[Utility &amp; Helpers](/categories/utility)

kento-oka/roust
===============

Roust is the fastest (wishful) URI router

v0.3.1(8y ago)128[1 issues](https://github.com/kento-oka/roust/issues)MITPHPPHP ^7.0

Since Nov 29Pushed 8y ago1 watchersCompare

[ Source](https://github.com/kento-oka/roust)[ Packagist](https://packagist.org/packages/kento-oka/roust)[ RSS](/packages/kento-oka-roust/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (5)Used By (0)

Roust
=====

[](#roust)

> *CAUTION*:
> This library has moved to [fratily/router](https://github.com/fratily/router). From now on plz use that.

Roust is the fastest (wishful) URI router.

> *NOTE*:
> I cant speak engilish. so I used GoogleTranslate.
> The exact REDME is in README\_origin.

Install
-------

[](#install)

Before using Roust in your project, execute it command in your project:

```
$ composer require 'kento-oka/roust'
```

Usage
-----

[](#usage)

First of all, create an instance of router.

```
use Roust\Router;

$router = new Router();
```

### Basic usage

[](#basic-usage)

```
use Roust\Router;
use Request;    //  Implemented Psr\Http\Message\ServerRequestInterface

$router     = new Router();
$request    = new Request();

//  It matches GET http://example.com/
$router->addRoute("GET", "/", [
    "controller"    => "index",
    "action"        => "index"
]);

//  It matches GET http://example.com/users/
$router->addRoute("GET", "/users/", [
    "controller"    => "user",
    "action"        => "index"
]);

//  It matches GET http://example.com/users/my/ and POST http://example.com/users/my/
$router->addRoute(["GET", "POST"], "users/my/", [
    "controller"    => "user",
    "action"        => "mypage"
]);

//  It matches GET http://example.com/users/123/
$router->addRoute("GET", "/users/{uid:[1-9][0-9]*}/", [
    "controller"    => "user",
    "action"        => "page"
]);

$result = $router->search($request->getMethod(), $request->getUri()->getPath());

switch($result["result"]){
    case Router::NOT_FOUND:
        // ... 404 Not Found
        break;
    case Router:METHOD_NOT_ALLOWED:
        $allowedMethods = $result["allowed"];
        // ... 405 Method Not Allowed
        break;
    case Router::FOUND:
        $params = $resul["params"];
        //  Do something
        break;
}
```

### Defining route

[](#defining-route)

Routing rules are defined by `Router::addRoute()`.

The first argument is an HTTP method to allow.
The second argument is a URI that matches.
And you can specify additional parameters for the third argument.

You can omit the leading slash in the second argument.

```
//  It matches GET http://example.com/
$router->addRoute("GET", "/", [
    "controller"    => "index",
    "action"        => "index"
]);

//  It matches GET http://example.com/users/
$router->addRoute("GET", "/users/", [
    "controller"    => "user",
    "action"        => "index"
]);

//  It matches GET http://example.com/users/my/ and POST http://example.com/users/my/
$router->addRoute(["GET", "POST"], "users/my/", [
    "controller"    => "user",
    "action"        => "mypage"
]);
```

Methods such as `Router::get()`, `Router::post()`, `Router::put()`, and `Router::delete()` are defined to omit specification of the HTTP method.

```
$router->get("/users/", [
    "controller"    => "user",
    "action"        => "index"
]);
```

#### Use regex

[](#use-regex)

Use the `{id:regex}` syntax to embed regular expression. *id* specifies the parameter name, *regex* specifies the regular expression to be used in PHP's preg\_match().

```
$router->addRoute("GET", "/users/{uid:[1-9][0-9]*}/", [
    "controller"    => "user",
    "action"        => "page"
]);
```

Even if the same parameter name is specified with the third argument, the value of second argument takes precedence.

#### Use short regex

[](#use-short-regex)

For example, if you add a routing rule that matches IP address, there would no one to write that regex in `Router::addRoute()`. Furthermore, if it is IPv4-mapped IPv6 address, it is useful if it can be converted to IPv4 at the routing stage.

Short regex that makes it possible.

It is short regex that matches only natural numbers and convert to int type:

```
$router->addShortRegex("d", new \Roust\Sregex\NaturalNumber());

$router->addroute("GET", "/users/{uid:|d}/", [
    "controller"    => "user",
    "action"        => "page"
]);
```

Short regex can be registered with `Router::addShortRegex()`.

The first argument is an qualified name.
The second argument will pass an instance of the class implementing `Roust\ShortRegexInterface`.

#### Group

[](#group)

In the example so far, there were parts common to some rules.

Taking the rule related to the user as an example, the head of URI is neccessarily '*/users/*' and the value of '*controller*' is '*user*'.

It is not hard to write common parts with the previous example. But Actually more rules are needed.

In Roust you can summarize the grouping of rules in this way:

```
$router->makePathGroup("/users", function($r){
    $r->get("/", [
        "controller"    => "user",
        "action"        => "index"
    ]);

    $r->makeParamsGroup(["Controller" => "user"], function($r){
        $r->get("/my/", [
            "action"    => "mypage"
        ]);

        $r->get("/{uid:[1-9][0-9]*}/", [
            "action"    => "page"
        ]);
    });
});
```

`Router::makePathGroup()` adds value of the first argument to the beginning of the URI of the rule added with `Router::addRoute()` only while the second argument callback is executed.

In `Router::makeParamsGroup()`, add parameters. This added parameters can be overwriten with `Router::addRoute()`.

Note
----

[](#note)

```
$route->addShortRegex("d", new NaturalNumber());
$router->get("/users/{id:[1-9][0-9]*}/", []);
$router->get("/users/{id:|d}/profile/", []);

$router->search("GET", "/users/123/");          // Not Found
$router->search("GET", "/users/123/profile");   // Found
```

String &gt; Short Regex &gt; Regex

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

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.

###  Release Activity

Cadence

Every ~4 days

Total

4

Last Release

3075d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9c0575d05e23e7ef9b1046a7a922927199c86ef292ebb188758bb214621d7efc?d=identicon)[kento-oka](/maintainers/kento-oka)

---

Top Contributors

[![kento-oka](https://avatars.githubusercontent.com/u/30544668?v=4)](https://github.com/kento-oka "kento-oka (26 commits)")

---

Tags

urirouter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kento-oka-roust/health.svg)

```
[![Health](https://phpackages.com/badges/kento-oka-roust/health.svg)](https://phpackages.com/packages/kento-oka-roust)
```

###  Alternatives

[league/uri-components

URI components manipulation library

31932.3M67](/packages/league-uri-components)[sabre/uri

Functions for making sense out of URIs.

29335.2M40](/packages/sabre-uri)[bramus/router

A lightweight and simple object oriented PHP Router

1.1k458.8k49](/packages/bramus-router)[laminas/laminas-uri

A component that aids in manipulating and validating » Uniform Resource Identifiers (URIs)

3834.9M89](/packages/laminas-laminas-uri)[cybercog/laravel-optimus

An Optimus bridge for Laravel. Id obfuscation based on Knuth's multiplicative hashing method.

192564.1k](/packages/cybercog-laravel-optimus)[opis/uri

Build, parse and validate URIs and URI-templates

1620.8M6](/packages/opis-uri)

PHPackages © 2026

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