PHPackages                             armaaar/mini-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. armaaar/mini-router

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

armaaar/mini-router
===================

Fast PHP router based on regular expressions

10PHP

Since Sep 6Pushed 1y agoCompare

[ Source](https://github.com/armaaar/mini-router)[ Packagist](https://packagist.org/packages/armaaar/mini-router)[ RSS](/packages/armaaar-mini-router/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

mini-router - Fast PHP regex based router
=========================================

[](#mini-router---fast-php-regex-based-router)

mini-router is a fast PHP router based on regular expressions inspired from [PHRoute](https://github.com/mrjgreen/phroute). It is build essentially to support RESTful APIs with simple, yet powerful, high speed interface.

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

[](#installation)

Install via composer or just include the `src/MiniRouter.php` file in your index page and start using it.

### Friendly URL

[](#friendly-url)

In order to handle all requests using the router, you need to redirect all requests to the page you would like to define your routes in. Create a simple .htaccess file on your root directory if you're using Apache with mod\_rewrite enabled.

```
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !\..+$
RewriteRule .* index.php [QSA,L]
```

Check the examples for implementation.

Usage
-----

[](#usage)

### Defining routes

[](#defining-routes)

mini-router supports GET, HEAD, POST, PUT, PATCH and DELETE methods.

```
use MiniRouter\MiniRouter;

$router = new MiniRouter();

$router->get($route, $handler);     # match only get requests
$router->post($route, $handler);    # match only post requests
$router->put($route, $handler);     # match only put requests
$router->delete($route, $handler);  # match only delete requests
$router->patch($route, $handler);   # match only patch requests
$router->head($route, $handler);    # match only head requests
$router->any($route, $handler);     # match any request method

$router->start_routing();
```

These methods are defined by the HTTP method the route must match, and accept the route pattern and a callable handler, which can be a closure, function name or \['ClassName', 'method'\] pair.

Note that the router doesn't by default echo the returned value from the handler, so if you want to send something back to the client you need to `echo` it, not to `return` it.

### Regex Shortcuts

[](#regex-shortcuts)

URLs can use regular expressions directly into the URL or use one of these shortcuts

```
{:i}  => ([0-9]+)              # numbers only
{:a}  => ([0-9A-Za-z]+)        # alphanumeric
{:h}  => ([0-9A-Fa-f]+)        # hex
{:s}  => ([a-zA-Z0-9+_\-\.]+)  # alphanumeric and + _ - . characters

```

Here are some examples of using regex and shortcuts in routes

```
  // you can pass parameters to controllers through URL using shortcuts
  $router->get('/hello/{:a}/', function($name) {
    echo "Hello, $name!";
  });

  // or you can use regular expressions directly into the URL
  $router->get('/hello-robot/(\d+)', function($robotNumber) {
    echo "Hello robot number $robotNumber!";
  });

  // URL parameters assigned to controller parameters in order
  // Note that regex can be used side by side with shortcuts
  $router->get('/hello-two/{:s}/([a-zA-Z0-9+_\-\.]+)', function($name1, $name2) {
    echo "Hello, $name1 and $name2!";
  });

  // Parameters could be optional but you need to define default values for it's corresponding variables
  $router->get('/hello-anon/{:a}?/', function($name = "anonymous") {
    echo "Hello, $name!";
  });
```

### Named Routes for Reverse Routing

[](#named-routes-for-reverse-routing)

Pass in an array as the first argument, where the first item is your route and the second item is a name to reference it later.

```
$router->get(['/user/{:a}', 'username'], function($name){
    echo 'Hello ' . $name;
});

$router->get(['/page/{:s}/{:i}', 'page'], function($slug, $id){
    echo 'You must be authenticated to see this page: ' . $id;
});

// Use the route name and pass any route parameters to redirect the browser to existing route path
// If you change your route path above, you won't need to go through your code updating any links/references to that route
$router->route('username', 'joe');
// this will redirect the browser to the path '/user/joe'

// if you passed a false value to the third argument, the browser will call the handler of the specified route without redirecting
$router->route('page', ['intro', 234], false);
// this will call the handler of the path '/page/intro/234'
```

### Groups

[](#groups)

Groups apply prefixes to URLS.

```
$router->group('/admin', function($router){

    $router->get('pages', function(){
        echo 'page management';
    });

    $router->get('products', function(){
        echo 'product management';
    });

    $router->get('orders', function(){
        echo 'order management';
    });
});
```

Groups can also define parameters in URL, parameters defined in a group is also passed to all group routes by default.

```
$router->group('/api/v{:i}', function($router, $api_version){

    $router->get('users', function($version){
        echo $version;
    });

    if ($api_version >= 2) {
        $router->get('order/{:i}', function($api_version, $order_id){
            echo $api_version;
        });
    }
});
```

It's good to notice that the router adds only the domain name at the beginning of any route. So if all your routes are inside a subfolder or subdirectory, group all your routes with the folder name as prefix.

### Filters

[](#filters)

You can Add a filter that must be true before a route can be accessed.

```
$router->filter('isLoggedIn', function(){
    if($_SESSION['logged_in']) {
      return true
    } else {
      return false
    }
});

$router->get('/user/{:a}', function($name){
    echo 'Hello ' . $name;
}, "isLoggedIn");
```

If the filter returns any falsy value, it will prevent the route handler from being dispatched. You can check falsy values from [PHP booleans documentation](https://secure.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting)

### Filter Groups

[](#filter-groups)

Wrap multiple routes in a route group to apply a filter to every route defined within. You can nest groups if required.

```
$router->filter('auth', function(){
    if(!isset($_SESSION['user']))
    {
        $router->route('login');
        return false;
    }
    return true;
});

$router->group('/portal', function($router){

    $router->get('/user/{:a}', function($name){
        echo 'Hello ' . $name;
    });

    $router->get('/page/{:i}', function($id){
        return 'You must be authenticated to see this page: ' . $id;
    });

}, "auth");

$router->get(['/login', 'login'], function($name){
    echo 'Please login...'
});
```

Check examples for more detailed practical use examples for mini-router, especially for RESTful APIs.

License
-------

[](#license)

[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

###  Health Score

14

—

LowBetter than 1% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

 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://avatars.githubusercontent.com/u/25823409?v=4)[Ahmed R. I.](/maintainers/armaaar)[@armaaar](https://github.com/armaaar)

---

Top Contributors

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

---

Tags

phpregular-expressionrouter

### Embed Badge

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

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

###  Alternatives

[compwright/codeigniter-installers

Composer installers for CodeIgniter

3484.3k4](/packages/compwright-codeigniter-installers)

PHPackages © 2026

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