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

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

kenphp/router
=============

Simple PHP Router

1.0.1(7y ago)1641MITPHPPHP &gt;=7.0CI failing

Since Nov 26Pushed 7y ago1 watchersCompare

[ Source](https://github.com/kenphp/router)[ Packagist](https://packagist.org/packages/kenphp/router)[ RSS](/packages/kenphp-router/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (1)Versions (3)Used By (1)

Router [![Build Status](https://camo.githubusercontent.com/0a931f35af2b8c52108100beeff5dd3951ee27bcd23e54b60c7bb277ae4f2d21/68747470733a2f2f7472617669732d63692e6f72672f6b656e7068702f726f757465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kenphp/router)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#router-)

A simple PHP Router for your web application. This library is part of KenPHP Project, but can be used independently.

Features
--------

[](#features)

- Static Route Patterns
- Dynamic Route Patterns
- Named parameters
- Optional parameters
- Regex-based route patterns
- Subrouting
- Supports web application routing
- Before and After route middleware
- Custom handler when a route is not found

What it does ?
--------------

[](#what-it-does-)

- Store route patterns, handlers, and middlewares information
- Resolve request to matching patterns

What it doesn't ?
-----------------

[](#what-it-doesnt-)

- Parse route path from $\_SERVER or any other means. You must provide the route path and method to `Router::resolve` method.
- Execute middlewares and handlers. It only returns an array containing matched route handlers, middlewares, and parameters found in the request.
- **Why isn't this library receives `Psr\Http\Message\RequestInterface` implementation and returns `Psr\Http\Message\ResponseInterface` ?**
    This library aims to gives as much freedom as possible to the user. Not everyone are using PSR-7 implementation and we want to respect that.

Requirements
------------

[](#requirements)

- PHP 7.0 or greater

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

[](#installation)

The easiest way to install is using Composer

```
$ composer require kenphp/router

```

Methods
-------

[](#methods)

1. `route($method, $route, $handler, $options = []) : void`

    Example :

    ```
    $router->route('GET', '/users', ['UserController', 'listUsers']);
    ```
2. `get($route, $handler, $options = []) : void`

    Example :

    ```
    $router->get('/users/{id}', ['UserController', 'getUser']);
    ```
3. `head($route, $handler, $options = []) : void`

    Example :

    ```
    $router->head('/users', ['UserController', 'listUsers']);
    ```
4. `post($route, $handler, $options = []) : void`

    Example :

    ```
    $router->post('/users', ['UserController', 'createUser']);
    ```
5. `put($route, $handler, $options = []) : void`

    Example :

    ```
    $router->put('/users/{id}', ['UserController', 'updateUser']);
    ```
6. `delete($route, $handler, $options = []) : void`

    Example :

    ```
    $router->delete('/users/{id}', ['UserController', 'deleteUser']);
    ```
7. `group($route, $fn, $options = []) : void`

    Example :

    ```
    $router->group('/api', function() use ($router) {
        $router->get('/products/{id}', ['ProductController', 'getProduct']);
    });
    ```
8. `setNotFoundHandler(callable $handler) : void`

    Example :

    ```
    $router->setNotFoundHandler(function() {
        echo 'Page not found.';
    });
    ```
9. `resolve($requestRoute, $method) : null|array`

    This function would return an array containing the following keys :

    - `handler`
    - `params`
    - Optional keys. This would be filled with any data from the `$options` parameter.

    Example :

    ```
    $router->get('/users/{id}', ['UserController', 'getUser'], [
        'namespace' => 'app\controllers'
    ]);

    $routeArray = $router->resolve('/users/1', 'GET');

    /**
     * $routeArray would contains
     * [
     *    'handler' => ['UserController', 'getUser'],
     *    'params' => ['id' => 1],
     *    'namespace' => 'app/controllers',
     * ]
     */
    ```

Examples
--------

[](#examples)

- [Usage example for web application](examples/index.php)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

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 ~61 days

Total

2

Last Release

2666d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.6

1.0.1PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6c34580489328b0909c2286319ed03cb235243f18a5d59dde55656d1862171c3?d=identicon)[juliardi](/maintainers/juliardi)

---

Top Contributors

[![juliardi](https://avatars.githubusercontent.com/u/4223302?v=4)](https://github.com/juliardi "juliardi (23 commits)")

###  Code Quality

TestsCodeception

### Embed Badge

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

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

###  Alternatives

[magepal/magento2-customeraccountlinksmanager

Customer Account Links Manager for Magento2 allows you to quickly and easily remove unwanted links from customer account dashboard

4084.9k](/packages/magepal-magento2-customeraccountlinksmanager)[nystudio107/craft3-multi-environment

Efficient and flexible multi-environment config for Craft 3 CMS

7218.3k5](/packages/nystudio107-craft3-multi-environment)[bazo/geotools

Geo Tools for PHP 5.4

1373.5k](/packages/bazo-geotools)[tomatophp/filament-helpers

Helper Class Generator to manage your forms and table inside your filament app

127.9k](/packages/tomatophp-filament-helpers)[trilobit-gmbh/contao-headerfootercode-bundle

Contao 4 / Contao 5 header- and footer code bundle

105.5k](/packages/trilobit-gmbh-contao-headerfootercode-bundle)

PHPackages © 2026

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