PHPackages                             pgf/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. [HTTP &amp; Networking](/categories/http)
4. /
5. pgf/router

ActiveLibrary[HTTP &amp; Networking](/categories/http)

pgf/router
==========

Fast request router for PHP

012PHP

Since May 8Pushed 11y ago1 watchersCompare

[ Source](https://github.com/msyagli/PGF-Router)[ Packagist](https://packagist.org/packages/pgf/router)[ RSS](/packages/pgf-router/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

PGF/Router - Fast request router for PHP
========================================

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

This library does not use regular expressions, instead routes are converted in tree-like structure (it is a native multi dimensional array). So later searches are very effective because complexity is related to the count of request URL segments not to number of added routes. Building the tree is an expensive task but structure can be cached very effectively. See simple benchmark below.

**This code is just a prove of concept.**Usage
----------------------------------------------

[](#this-code-is-just-a-prove-of-conceptusage)

Basic usage:

```
include './vendor/autoload.php';
$router = new \PGF\Router\Router();
$router->addRoute('get', '/', 'HomeController@index');
$router->addRoute('get', '/user/', 'UserController@index');
$router->addRoute('get', '/user/{id}', 'UserController@show');
$router->addRoute('post', '/user/{id}', 'UserController@save');
//{user?} marks optional parameter
$router->addRoute('get', '/message/send/{user?}', 'MessagesController@send');
$router->findRoute('get', '/message/send/John')
```

Adding routes
-------------

[](#adding-routes)

Routes are added using:

```
addRoute($method, $route, $action)

```

#### Methods

[](#methods)

Available methods are: \[get,post,put,delete, any\]. A route can have more than one method:

```
$router->addRoute(['get','post'], '/user/', 'UserController@index');

```

#### findRoute

[](#findroute)

If a route is found an array is returned. Structure is:

```
$router->findRoute('get', '/user/1/');
Array
(
    [route] => /user/{id} // registered route
    [method] => get // route method
    [action] => UserController@show // action for this route
    [params] => Array // route parameters with there names and values. Order is same as they are found in route.
    (
        [id] => 1
    )
)

```

#### Caching

[](#caching)

Because internal structure is plain multidimensional array, caching is very easy and effective. Data for caching can be obtain using:

```
$router->dump();

```

And can be loaded using:

```
$router->load($cachedArray);

```

Using **load()** method will overwrite any previously added routes. If you load data adding routes using **addRoute()** is not necessary

#### Exceptions

[](#exceptions)

All exceptions are in **PGF\\Router\\Exceptions** namespace.

**InvalidMethodException** is thrown when route is registered with invalid method.

**MethodNotAllowedException** is thrown when route is found but requested method is not allowed.

**RouteNotFoundException** is thrown when route is not found.

#### Optional parameters issues

[](#optional-parameters-issues)

Using optional parameters can cause some issues. For example:

```
$router->addRoute('get', '/', 'HomeController@index');
$router->addRoute('get', '/{id}', 'HomeController@show');
$router->addRoute('get', '/{id?}', 'HomeController@get');

```

**HomeController@get** will never match because if request URL is **/** then **HomeController@index** will be returned. If request URL is **/123** then **HomeController@show** will be returned.

#### Performance

[](#performance)

Tests are executed using Apache Benchmark and show requests/second:

```
ab -n 10000 -c 50

```

| PGF/Router | nikic/fast-route --- | --- | --- 1 route (not cached - found) | 16400 | 13300 1 route (not cached - not found) | 14400 | 13700 30 routes (not cached - found) | 9400 | 6800 30 routes (not cached - not found) | 9200 | 6600 100 routes (not cached - found) | 4900 | 2800 100 routes (not cached - not found) | 4800 | 2700 100 routes (cached - found) | 10800 | 12000 100 routes (cached - not found) | 9900 | 9100 As you can see from results this implementation is very close to one of the fastest routing library.

#### Issues

[](#issues)

- Currently no validation or routes constrains is implemented.
- Reverse process of generating URL from route and parameters is not implemented.
- In some corner cases optional parameters may not work as you expect, but behavior is constant between requests.
- Code coverage is not enough.

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6135746?v=4)[Sait](/maintainers/msyagli)[@msyagli](https://github.com/msyagli)

---

Top Contributors

[![gatakka](https://avatars.githubusercontent.com/u/248079?v=4)](https://github.com/gatakka "gatakka (9 commits)")

### Embed Badge

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

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

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25025.5M80](/packages/php-http-cache-plugin)[illuminate/http

The Illuminate Http package.

11937.2M6.6k](/packages/illuminate-http)[rdkafka/rdkafka

A PHP extension for Kafka

2.2k20.0k1](/packages/rdkafka-rdkafka)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

87930.4k113](/packages/httpsoft-http-message)[mezzio/mezzio-router

Router subcomponent for Mezzio

265.3M84](/packages/mezzio-mezzio-router)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69122.6k](/packages/serpapi-google-search-results-php)

PHPackages © 2026

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