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

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

jblond/php-router
=================

A simple php router class

v3.0.0(2y ago)1381MITPHPPHP &gt;= 8.1

Since Feb 1Pushed 1y ago1 watchersCompare

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

READMEChangelog (7)Dependencies (1)Versions (8)Used By (0)

PHP router
==========

[](#php-router)

[![Code Climate](https://camo.githubusercontent.com/fbfcbf5f17056f3911523d9c18b603306db7c9ee46529c11e5141d289370094f/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f4a426c6f6e642f7068702d726f757465722f6261646765732f6770612e737667)](https://codeclimate.com/github/JBlond/php-router) [![Codacy Badge](https://camo.githubusercontent.com/735f7f089db46969b7c2118bc2a404e1692f683665c8c8b4cbe76551c6de628f/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f6138393030356639386134383463326462326261613833326335626435373362)](https://www.codacy.com/app/leet31337/php-router?utm_source=github.com&utm_medium=referral&utm_content=JBlond/php-router&utm_campaign=Badge_Grade)

```
composer require jblond/php-router
```

A simple PHP router class
-------------------------

[](#a-simple-php-router-class)

Less than 300 lines of code with comments

Supports

- lambda URLs:
    - **:any** Any sign
    - **:num** Only numbers
    - **:all** All characters
    - **:an** A-Z a-z 0-9
    - **:url** A-Z a-z 0-9 - \_
    - **:hex** hexadecimal
- Regex URLs e.g. /user/(.\*)/edit
- Optional Subroutes
- Works in Subdirs, if you use `->set_basepath('/yoursubdir')`
- Custom response headers
    - download
    - header(s)
    - redirect
    - 503 error

Install
-------

[](#install)

```
composer require jblond/php-router

```

Examples
--------

[](#examples)

### Static routes

[](#static-routes)

```
require 'jblond/Autoloader.php';
new \jblond\Autoloader();
$router = new \jblond\router\Router();
$router->setBasepath('');
$router->init();

$router->add('/', function () {
    echo 'Welcome';
});

$router->add('/info/', function () {
    phpinfo();
});

$router->add('/test.html', function () {
    echo 'test.html Welcome';
});
$router->add('/post/', function () {
	require 'post.html';
});

$router->post('/post/reciver/', function () {
    print_r($_POST);
});
$router->run();
```

### dynamic routes

[](#dynamic-routes)

```
$router->add('/user/(.*)/edit', function ($id) {
    echo 'Edit user with id ' . $id;
});

$router->get('/test/(:any)', function () {
    print_r(filter_input(INPUT_SERVER, 'REQUEST_URI'));
});

```

### Integration with other libraries aka using closure

[](#integration-with-other-libraries-aka-using-closure)

```
$tpl = new \Acme\Template\Template();
$router->add('/closure', function () use ($tpl) {
    // $tpl->...
    echo 'closure';
});
```

### Define 404 not found page

[](#define-404-not-found-page)

```
$router->add404(function ($url) {
    header("HTTP/1.0 404 Not Found");
    echo '404 :-( ' . $url;
});
$router->run();
```

### Responses

[](#responses)

```
$router->get('/503/', function (){
    $response = new \jblond\router\Responses();
    $response->error503();
});
```

### Optional Subpatterns

[](#optional-subpatterns)

Optional route subpattern can be made of using `?` aftern the normal pattern.

```
$router->get(
    '/phonebook(\/[A-Za-z]+(\/[A-Za-z]+(\/[A-Za-z]+(\/[0-9-]+)?)?)?)?/',
    function ($lastname = null, $surname = null, $street = null, $number = null) {
        if(!$lastname) {
            echo 'Phonebook all entries';
            return;
        }
        if(!$surname){
            echo 'Phonebook lookup lastname: ' . $lastname;
            return;
        }
        if(!$street){
            echo 'Phonebook lookup lastname: ' . $lastname . ' Surname: ' . $surname;
            return;
        }
        if(!$number){
            echo 'Phonebook lookup lastname: ' . $lastname . ' Surname: ' . $surname . ' Street: ' . $street;
            return;
        }
        echo ' FULL SEARCH';
});
```

For the regexpattern see

Also good for testing your regex:  use preg\_match

### Apache rewrite config

[](#apache-rewrite-config)

```
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

```

with apache 2.4 you can use

```
FallbackResource /index.php

```

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity74

Established project with proven stability

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

Recently: every ~311 days

Total

7

Last Release

939d ago

Major Versions

v1.0.2 → v2.0.02020-07-14

v2.0.2 → v3.0.02023-12-05

PHP version history (3 changes)v1.0.2PHP &gt;= 7.0

v2.0.0PHP &gt;= 7.1

v3.0.0PHP &gt;= 8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/e29eedf6a63367a7c0841f304be766cb6745cd798ff066a00d342ada47e507b2?d=identicon)[JBlond](/maintainers/JBlond)

---

Top Contributors

[![JBlond](https://avatars.githubusercontent.com/u/2391810?v=4)](https://github.com/JBlond "JBlond (85 commits)")

---

Tags

phpphp-routerrouterphprouter

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

PHPackages © 2026

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