PHPackages                             patrikmokry/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. [API Development](/categories/api)
4. /
5. patrikmokry/router

ActiveLibrary[API Development](/categories/api)

patrikmokry/router
==================

A fast PHP router, with route parameters

3191PHP

Since Jan 27Pushed 7y agoCompare

[ Source](https://github.com/MokryPatrik/PHP-Router)[ Packagist](https://packagist.org/packages/patrikmokry/router)[ RSS](/packages/patrikmokry-router/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

PHP Router - Fast router for PHP
================================

[](#php-router---fast-router-for-php)

[![Total Downloads](https://camo.githubusercontent.com/13b1065b2dbc928bda5bf479482c5d26d56bdc314f10ed1db88d936b1f67d5aa/68747470733a2f2f706f7365722e707567782e6f72672f70617472696b6d6f6b72792f726f757465722f646f776e6c6f616473)](https://packagist.org/packages/patrikmokry/router) [![License](https://camo.githubusercontent.com/936fe2bd53a0cc5abd60f56a30a7a4b2f69f62acb188e982fb2cc833a49fad76/68747470733a2f2f706f7365722e707567782e6f72672f70617472696b6d6f6b72792f726f757465722f6c6963656e7365)](https://packagist.org/packages/patrikmokry/router) [![Latest Version](https://camo.githubusercontent.com/ace1f3aadfa3141006623929125ad2c63b83be8ff0e88faf76d3d082ec1d70ff/68747470733a2f2f706f7365722e707567782e6f72672f70617472696b6d6f6b72792f726f757465722f762f756e737461626c65)](https://packagist.org/packages/patrikmokry/router)

A lightweight and simple object oriented PHP Router with support of Controllers.

This library provides a fast implementation of a regular expression based router.
---------------------------------------------------------------------------------

[](#this-library-provides-a-fast-implementation-of-a-regular-expression-based-router)

- [Defining routes](#defining-routes)
- [Regex Shortcuts](#regex-shortcuts)
- [Named Routes for Reverse Routing ](#named-routes-for-reverse-routing)
- [Prefix Groups](#prefix-groups)

Easy to install with composer
-----------------------------

[](#easy-to-install-with-composer)

Install via composer

```
composer require patrikmokry/router

```

Usage
-----

[](#usage)

### Example

[](#example)

```
// index.php

use PatrikMokry\Router;

require_once __DIR__ . '/vendor/autoload.php';

Router::get('example', function() {
    return 'This route responds to requests with the GET method at the path /example';
});
Router::get('example/{id}', function($id) {
    return 'This route responds to requests with the GET method at the path /example/';
});
Router::get('example/{id}?', function() {
    return 'This route responds to requests with the GET method at the path /example/[optional]';
});
Router::post('example', function() {
    return 'This route responds to requests with the POST method at the path /example';
});

Router::execute(__DIR__);
```

```
// .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php [QSA,L]
```

### Defining Routes

[](#defining-routes)

```
use PatrikMokry\Router;

Router::get($route, $action);
Router::post($route, $action);
Router::put($route, $action);
Router::delete($route, $action);
Router::any($route, $action);
```

> These helper methods are wrappers around `route($route, $action, $method = ["POST", "GET"])`
> Neither `$_PUT` nor `$_DELETE` does not exist so in your request you must define field with name `_method`

### Regex Shortcuts

[](#regex-shortcuts)

```
:i => (\d+)   # numbers only
:s => (\w+)   # any word, character

use in routes:

'/user/{name::i}'
'/user/{name::s}'

```

##### Custom shortcuts

[](#custom-shortcuts)

```
Router::addShortcut(name, regex)

// create shortcut with default value
Router::addShortcut('locale', '(sk|en)->sk)')
```

### Named Routes for Reverse Routing

[](#named-routes-for-reverse-routing)

```
Router::get('example/{id}', function() {
	return 'example';
})->name('example');

echo Router::link('example', ['id' => 48]) // example/48
```

### Prefix Groups

[](#prefix-groups)

```
// If you need some prefix e.g. admin, api, locale, ...

Router::prefix('admin/', function() {
	Router::get('example/{id}', function() {
		return 'example';
	});
});
```

### Contributing

[](#contributing)

1. Fork it (  )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/31004308?v=4)[Patrik Mokrý](/maintainers/MokryPatrik)[@MokryPatrik](https://github.com/MokryPatrik)

---

Top Contributors

[![MokryPatrik](https://avatars.githubusercontent.com/u/31004308?v=4)](https://github.com/MokryPatrik "MokryPatrik (6 commits)")

---

Tags

frameworkphpphp-frameworkphprouterrouter

### Embed Badge

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

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

###  Alternatives

[exsyst/swagger

A php library to manipulate Swagger specifications

35816.3M7](/packages/exsyst-swagger)[hubspot/api-client

Hubspot API client

24015.5M18](/packages/hubspot-api-client)[pocketmine/bedrock-protocol

An implementation of the Minecraft: Bedrock Edition protocol in PHP

172437.8k11](/packages/pocketmine-bedrock-protocol)[botman/driver-telegram

Telegram driver for BotMan

93452.6k6](/packages/botman-driver-telegram)

PHPackages © 2026

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