PHPackages                             itlessons/php-routing - 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. itlessons/php-routing

ActiveLibrary[Framework](/categories/framework)

itlessons/php-routing
=====================

PHP Routing Library

0.0.1(11y ago)2114461MITPHPPHP &gt;=5.3.3

Since Jan 17Pushed 11y ago4 watchersCompare

[ Source](https://github.com/itlessons/php-routing)[ Packagist](https://packagist.org/packages/itlessons/php-routing)[ Docs](https://github.com/itlessons/php-routing)[ RSS](/packages/itlessons-php-routing/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)DependenciesVersions (2)Used By (1)

PHP Routing Library
===================

[](#php-routing-library)

Routing associates a request with the code that will convert it to a response.

The example below demonstrates how you can set up a fully working routing system:

```
use Routing\Router;

$host = 'http://domain.tld';

$router = new Router($host);

$router->add('home', '/', 'controller:action');
$router->add('hello', '/hello', 'static:welcome', 'GET');
$router->add('profile', '/user(id:num)', 'profile:index', 'GET|POST');

$route = $router->match('GET', '/user777');
// $route->getController() => 'static:welcome'
// $route->getParameters() => [id:777]

$url = $router->generate('profile', array('id' => 777));
// $url => /user777

$url = $router->generate('profile', array('id' => 777), true);
// $url => http://domain.tld/user777

```

URL Matching Only
-----------------

[](#url-matching-only)

You can use url matcher standalone:

```
use Routing\UrlMatcher;

$matcher = new UrlMatcher();
$matcher->register('GET', '/', 'controller:action');
$matcher->register('GET', '/hello', 'static:welcome');
$matcher->register('GET|POST', '/user(id:num)', 'profile:index');

$route = $router->match('GET', '/hello');

```

URL Generating Only
-------------------

[](#url-generating-only)

You can use url generator standalone:

```
use Routing\UrlGenerator;

$generator = new UrlGenerator('http://domain.tld');
$generator->add('home', '/');
$generator->add('hello', '/hello');
$generator->add('profile', '/user(:id)');

$url = $generator->generate('profile', array('id' => 888), true);

```

Optional Last Placeholder
-------------------------

[](#optional-last-placeholder)

You can specify optional placeholder in the end of pattern:

```
use Routing\Router;

$host = 'http://domain.tld';

$router = new Router($host);
$router->add('blog', '/blog/(page:num:?)', 'controller:action');

// match
$route = $router->match('GET', '/blog');
$route = $router->match('GET', '/blog/1');

// generate
$router->generate('blog'); => /blog
$router->generate('blog',  array('page' => 1)); => /blog/1

```

Similar Routes
--------------

[](#similar-routes)

You can use redirect on similar route (e.g /blog -&gt; /blog/ if /blog/ exists):

```
use Routing\Router;

$host = 'http://domain.tld';

$router = new Router($host);
$router->add('home', '/', 'controller:action');
$router->add('hello', '/hello', 'static:welcome');
$router->add('profile', '/blog/', 'profile:index');

$route = $router->match('GET', '/hello/');
if($router->getMatcher()->isNeedRedirect()){
    // need redirect to /hello
    redirect($router->getMatcher()->getRedirectUrl(), 302);
}

$route = $router->match('GET', '/blog');
if($router->getMatcher()->isNeedRedirect()){
    // need redirect to /blog/
    redirect($router->getMatcher()->getRedirectUrl(), 302);
}

```

Cache Compiled Data
-------------------

[](#cache-compiled-data)

You can cache compiled rules to files for performance:

```
use Routing\Router;
use Routing\Request;

$request = new Request();

$router = new Router($request->getHTTPHost());
$router->useCache(__DIR__.'/matcher.cache.php', __DIR__.'/generator.cache.php');
$router->add('home', '/', 'controller:action');
// ...
$route = $router->match($request->getMethod(), $request->getPathInfo());

```

Request Class Helper
--------------------

[](#request-class-helper)

You can use simple request class to find pathInfo:

```
use Routing\Router;
use Routing\Request;

$request = new Request();

$router = new Router($request->getHTTPHost());

$router->add('home', '/', 'controller:action');
$router->add('hello', '/hello', 'static:welcome', 'GET');
$router->add('profile', '/user(id:num)', 'profile:index', 'GET|POST');

$route = $router->match($request->getMethod(), $request->getPathInfo());

```

Resources
---------

[](#resources)

You can run the unit tests with the following command:

```
$ cd path/to/php-routing/
$ composer.phar install
$ phpunit

```

Links
-----

[](#links)

- [Система роутинга на сайте с помощью PHP](http://www.itlessons.info/php/routing-library/)
- [Base example source](http://demos.itlessons.info/res/024-php-routing.zip)

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity48

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

Unknown

Total

1

Last Release

4183d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7283c22f1b152268a8a17acd947f00c4f65f41401a34407f2319de57cca45fb8?d=identicon)[itlessons](/maintainers/itlessons)

---

Top Contributors

[![itlessons](https://avatars.githubusercontent.com/u/5404185?v=4)](https://github.com/itlessons "itlessons (12 commits)")

---

Tags

urlurirouterrouting

### Embed Badge

![Health badge](/badges/itlessons-php-routing/health.svg)

```
[![Health](https://phpackages.com/badges/itlessons-php-routing/health.svg)](https://phpackages.com/packages/itlessons-php-routing)
```

###  Alternatives

[symfony/routing

Maps an HTTP request to a set of configuration variables

7.6k819.6M2.3k](/packages/symfony-routing)[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.

675224.9k18](/packages/pecee-simple-router)[vlucas/bulletphp

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

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

simple router class for php

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

A quick and snappy routing framework.

4615.2k](/packages/vectorface-snappy-router)

PHPackages © 2026

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