PHPackages                             andrewcarteruk/simple-route - 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. andrewcarteruk/simple-route

ActiveLibrary

andrewcarteruk/simple-route
===========================

An easy to use wrapper for the FastRoute package

v0.3.0(9y ago)162151MITPHPPHP &gt;=5.4

Since Jan 26Pushed 9y ago2 watchersCompare

[ Source](https://github.com/AndrewCarterUK/SimpleRoute)[ Packagist](https://packagist.org/packages/andrewcarteruk/simple-route)[ RSS](/packages/andrewcarteruk-simple-route/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

SimpleRoute
===========

[](#simpleroute)

[![Build Status](https://camo.githubusercontent.com/12a5615c6cf73267e549c9f85c7c45db1428c7a892f279332573e5c79b8f2a1a/68747470733a2f2f7472617669732d63692e6f72672f416e64726577436172746572554b2f53696d706c65526f7574652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/AndrewCarterUK/SimpleRoute)[![Code Coverage](https://camo.githubusercontent.com/ca3ffb39ad5851dd4e329e283284354274f4a1f94c7b144e3b05c7b598728514/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f416e64726577436172746572554b2f53696d706c65526f7574652f6261646765732f636f7665726167652e706e673f623d6d6173746572266e6f2d63616368653d31)](https://scrutinizer-ci.com/g/AndrewCarterUK/SimpleRoute/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e7307e6891dae7ee6039d9676d9f3d374a83c332c0abc21901577833de80d792/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f416e64726577436172746572554b2f53696d706c65526f7574652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/AndrewCarterUK/SimpleRoute/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/8165f460197b6f0a27d3a16a965e704306c508b7f1a9c28ebef243f3d4524035/68747470733a2f2f706f7365722e707567782e6f72672f616e64726577636172746572756b2f73696d706c652d726f7574652f762f737461626c65)](https://packagist.org/packages/andrewcarteruk/simple-route)[![Total Downloads](https://camo.githubusercontent.com/99fa50766bbc5d7f71945d9b3c32ed6abded646870747a8ee48a41d8d194b98d/68747470733a2f2f706f7365722e707567782e6f72672f616e64726577636172746572756b2f73696d706c652d726f7574652f646f776e6c6f616473)](https://packagist.org/packages/andrewcarteruk/simple-route)[![License](https://camo.githubusercontent.com/c29df6b9b2b3d105cd07238598a1bf60b1421e10bccdfda43584c9f4e6e093ae/68747470733a2f2f706f7365722e707567782e6f72672f616e64726577636172746572756b2f73696d706c652d726f7574652f6c6963656e7365)](https://packagist.org/packages/andrewcarteruk/simple-route)

This easy to use router is a simple wrapper for the [FastRoute](https://github.com/nikic/FastRoute) library.

By [AndrewCarterUK ![(Twitter)](https://camo.githubusercontent.com/6bdedfb2ec11bea20e0fb9720641fd26240465c2361f25955ca995df499d5d20/687474703a2f2f692e696d6775722e636f6d2f77577a583975422e706e67)](https://twitter.com/AndrewCarterUK)

Install
-------

[](#install)

Install using [Composer](https://getcomposer.org).

```
composer require andrewcarteruk/simple-route ^0.2
```

Example Usage
-------------

[](#example-usage)

```
use SimpleRoute\Route;
use SimpleRoute\Router;

$router = Router::fromArray([
    new Route('GET', '/', 'handler1'),
    new Route('GET', '/{page}', 'handler2'),
]);

try {
    $result = $router->match($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

    $handler = $result->getHandler();
    $params = $result->getParams();

    // ...
} catch (SimpleRoute\Exception\NotFoundException $exception) {
    // ...
} catch (SimpleRoute\Exception\MethodNotAllowedException $exception) {
    // ...
}
```

Documentation
-------------

[](#documentation)

*The following documentation is derived from the [FastRoute](https://github.com/nikic/FastRoute#defining-routes)documentation.*

Routes are defined as an array of `SimpleRoute\Route` objects that are passed to `SimpleRoute\Router::fromArray(array $routes)`.

`SimpleRoute\Route` objects require a `$method`, a `$pattern` and a `$handler`:

```
$route = new Route($method, $pattern, $handler);
```

The `$method` is an uppercase HTTP method string for which a certain route should match. It is possible to specify multiple valid methods using an array:

```
$routes = [
    // This route
    new Route(['GET', 'POST'], '/test', 'handler'),

    // Is equivalent to these two routes together
    new Route('GET', '/test', 'handler'),
    new Route('POST', '/test', 'handler'),
];
```

By default the `$pattern` uses a syntax where `{foo}` specifies a placeholder with name `foo` and matching the regex `[^/]+`. To adjust the pattern the placeholder matches, you can specify a custom pattern by writing `{bar:[0-9]+}`.

Some examples:

```
$routes = [
    // Matches /user/42, but not /user/xyz
    new Route('GET', '/user/{id:\d+}', 'handler'),

    // Matches /user/foobar, but not /user/foo/bar
    new Route('GET', '/user/{name}', 'handler'),

    // Matches /user/foo/bar as well
    new Route('GET', '/user/{name:.+}', 'handler'),
];
```

Custom patterns for route placeholders cannot use capturing groups. For example `{lang:(en|de)}` is not a valid placeholder, because `()` is a capturing group. Instead you can use either `{lang:en|de}` or `{lang:(?:en|de)}`.

Furthermore parts of the route enclosed in `[...]` are considered optional, so that `/foo[bar]` will match both `/foo` and `/foobar`. Optional parts are only supported in a trailing position, not in the middle of a route.

```
$routes = [
    // This route
    new Route('GET', '/user/{id:\d+}[/{name}]', 'handler'),

    // Is equivalent to these two routes together
    new Route('GET', '/user/{id:\d+}', 'handler'),
    new Route('GET', '/user/{id:\d+}/{name}', 'handler'),

    // This route is NOT valid, because optional parts can only occur at the end
    new Route('GET', '/user[/{id:\d+}]/{name}', 'handler'),
];
```

The `$handler` parameter does not necessarily have to be a callback, it could also be a controller class name or any other kind of data you wish to associate with the route. SimpleRoute only tells you which handler corresponds to your URI, how you interpret it is up to you.

### Credits

[](#credits)

This library is merely a wrapper for [FastRoute](https://github.com/nikic/FastRoute)that aims to provide an easier to use API.

The author of [FastRoute](https://github.com/nikic/FastRoute) is [Nikita Popov](http://nikic.github.io/).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community9

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

Total

4

Last Release

3615d ago

### Community

Maintainers

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

---

Top Contributors

[![AndrewCarterUK](https://avatars.githubusercontent.com/u/6486835?v=4)](https://github.com/AndrewCarterUK "AndrewCarterUK (22 commits)")

---

Tags

routerroutingSimplefastroute

### Embed Badge

![Health badge](/badges/andrewcarteruk-simple-route/health.svg)

```
[![Health](https://phpackages.com/badges/andrewcarteruk-simple-route/health.svg)](https://phpackages.com/packages/andrewcarteruk-simple-route)
```

###  Alternatives

[aura/router

Powerful, flexible web routing for PSR-7 requests.

5231.5M67](/packages/aura-router)[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)[pmjones/auto-route

Automatically routes HTTP request to action classes.

20158.6k6](/packages/pmjones-auto-route)[miladrahimi/phprouter

A powerful, lightweight, and very fast HTTP URL router for PHP projects.

20832.6k2](/packages/miladrahimi-phprouter)[izniburak/router

simple router class for php

23522.6k7](/packages/izniburak-router)[contributte/api-router

RESTful Router for your Apis in Nette Framework - created either directly or via attributes

20802.8k3](/packages/contributte-api-router)

PHPackages © 2026

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