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

ActiveLibrary[Framework](/categories/framework)

karmabunny/router
=================

Routing library

v2.8.17(2mo ago)01.4k↓44.1%12MITPHPCI passing

Since Jun 7Pushed 2mo ago7 watchersCompare

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

READMEChangelogDependencies (3)Versions (30)Used By (2)

KB Router
=========

[](#kb-router)

This is a (slightly) opinionated path router.

The opinion
-----------

[](#the-opinion)

Many path routers provide the full power of the regex engine to the route table. Allowing one to write any magical incantation they like. Sometimes because they're lazy or maybe they think that it's necessary.

*I believe* that routes are not complex. They are simple path patterns with a few simple variables.

The role of validating those variables is *not* in the hands of a router. This is the role of the controller. For example, an end-user needs to be told 'this is not a valid ID'. If you wrote `'/user/([0-9]+)/view'` as your route, the route would never detect these invalid IDs and the user would instead receive 'page not found'. Not helpful.

The solution
------------

[](#the-solution)

A route 'rule' is a simple syntax.

E.g. `/user/{id}/view/*`

Curly brackets `{}` contain variable names. These follow the same rules as PHP variables + regex group names. That is; `[a-z][a-z0-9_]+`. They only capture between path delimiters. The value will never contain a forward slash - `/`.

Wildcards `*` are bit looser. They are unnamed and can contain anything. You can have as many as you like but at the risk of a very messy result.

Modes
-----

[](#modes)

This package provides three implementation of a router.

### 1. Chunked Group Based

[](#1-chunked-group-based)

This is an efficient method of executing route patterns in bulk, as described here:

### 2. Simple Mode

[](#2-simple-mode)

This is a base implementation of rule patterns.

### 3. Regex Mode

[](#3-regex-mode)

This mode provides the full regex engine as a route pattern. This is incompatible with the rule patterns used in the chunked + simple modes.

This is a *transitional* mode for old projects. I don't intend anyone to use this for long periods of time.

Install
-------

[](#install)

Install with composer:

`composer require karmabunny/router`

Usage
-----

[](#usage)

```
use karmabunny\router\Router;

// Create a router with a config.
$router = Router::create([]);

// Load some routes.
// These are keyed:
// [ rule => target ]
$routes = include __DIR__ . '/routes.php';
$router->load($routes);

// Perform routing.
$action = $router->find('GET', '/user/123/edit');

if (!$action) {
    die('not found');
}

// The route target, as defined in the route table.
echo $action->target, PHP_EOL;

// The path arguments. Keyed by name. Wildcards are always last.
echo $action->args, PHP_EOL;
```

The router doesn't explcitly require a "controller" concept, nor methods or anything. Simply it returns the target provided in the rule table.

However, the `Action` class does provide some helpers to execute functions or controller methods.

```
// Assert the target is callable:
// either a plain method, closure, or [class, method].
$action->isCallable();

// Assert the target is in the form of [class, method].
if ($action->isController(MyController::class)) {
    $controller = $action->createController();
    $result = $action->invoke($controller);
}
else {
    // For everything else.
    $result = $action->invoke();
}
```

Route table
-----------

[](#route-table)

The route table can be built by hand as always. This a keyed array of rules to target. A 'target' is typically a callable.

This is loaded into the router with `load($routes)`.

The router itself has no preference how you manage your targets. That said, this package contains a bunch of utilities that assumes that your target is a callable, typically within a controller.

### Route Discovery

[](#route-discovery)

Instead of building your route table, perhaps try *route discovery*. This a method where one can write their routes right next to their controller actions.

Like so:

```
use karmabunny/route/Route;

class MyController
{
    #[Route('GET /my/php8/{route}')]
    public function php8Action() {}

    /** @route GET /my/php7/{route} */
    public function php7Action() {}
}
```

If you're gifted with a PHP8 environment, you can use the new attributes feature! Otherwise the `@route` tag always works.

### Action Routes

[](#action-routes)

TODO

These are routes generated from namespaces.

I can't yet decide *how* magic these will be. All public methods? Or only tagged methods? Or perhaps exclude some methods? Only POST/PUT methods with an action param and security token? Idk!

Config
------

[](#config)

- `'mode'` - one of: single|chunked|regex
- `'case_insensitive'` - boolean
- `'methods'` - array valid methods
- `'chunk_size'` - for chunked mode, default 10

See `src/RouterConfig.php`.

TODO
----

[](#todo)

- some sort of caching
- ACTION method?
- something about unicode?
- compat attribute loader (for sprout3)
    - converts rule syntax to regex
    - inserts targets as `ns\to\class/method/arg1/arg2`

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance84

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 99.2% 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 ~66 days

Recently: every ~279 days

Total

27

Last Release

86d ago

Major Versions

v1.5.6 → v2.5.62021-12-18

v1.5.7 → v2.6.72022-01-12

### Community

Maintainers

![](https://www.gravatar.com/avatar/11b33084210490439a6fb2c0b277e0bf39f8d0e368f20bebb703834003a617df?d=identicon)[karmabunny](/maintainers/karmabunny)

---

Top Contributors

[![gwillz](https://avatars.githubusercontent.com/u/3466850?v=4)](https://github.com/gwillz "gwillz (126 commits)")[![benno-truevault](https://avatars.githubusercontent.com/u/247051752?v=4)](https://github.com/benno-truevault "benno-truevault (1 commits)")

---

Tags

routerrouting

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[klein/klein

A lightning fast router for PHP

2.7k1.1M31](/packages/klein-klein)[pecee/simple-router

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

696214.6k17](/packages/pecee-simple-router)[vlucas/bulletphp

A heierarchical resource-oriented micro-framework built on nested closures instead of route-based callbacks

41949.9k1](/packages/vlucas-bulletphp)[izniburak/router

simple router class for php

23522.6k7](/packages/izniburak-router)[vectorface/snappy-router

A quick and snappy routing framework.

4614.7k](/packages/vectorface-snappy-router)[thewunder/croute

Convention based routing for PHP

1317.4k](/packages/thewunder-croute)

PHPackages © 2026

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