A lightweight and fast router for PHP

Overview

Piko Router

build Coverage Status

A lightweight and blazing fast router (see benchmarks) using a radix trie to store dynamic routes.

This router maps routes to user defined handlers and can do the reverse operation (reverse routing).

Installation

It's recommended that you use Composer to install Piko Router.

composer require piko/router

Usage

A basic example:

use piko\Router;

$router = new Router();
$router->addRoute('/', 'homeView');
$router->addRoute('/user/:id', 'userView');

$match = $router->resolve('/');
echo $match->handler; // homeView

$match = $router->resolve('/user/10');
echo $match->handler; // userView
echo $match->params['id']; // 10

// Use of the $match->handler to dispatch an action
// ...

// Reverse routing
echo $router->getUrl('homeView'); // /
echo $router->getUrl('userView', ['id' => 3]); // /user/3

Dynamic handlers:

use piko\Router;

$router = new Router();
$router->addRoute('/admin/:module/:action', ':module/admin/:action');

$match = $router->resolve('/admin/user/add');
echo $match->handler; // user/admin/add

echo $router->getUrl('blog/admin/index'); // /admin/blog/index

Advanced usage: See RouterTest.php

Benchmarks

Piko router comparison against Fastroute (cached) and Symfony router (cached).

Benchmark against 1000 generated routes

./vendor/bin/phpbench run --revs=10000 --report='extends:aggregate,break:["benchmark"]'
SymfonyRouter
+--------------------+--------------+-------+-----+----------+---------+--------+
| subject            | set          | revs  | its | mem_peak | mode    | rstdev |
+--------------------+--------------+-------+-----+----------+---------+--------+
| benchStaticRoutes  | Best Case    | 10000 | 5   | 7.241mb  | 1.243μs | ±2.00% |
| benchStaticRoutes  | Average Case | 10000 | 5   | 7.241mb  | 1.296μs | ±2.40% |
| benchStaticRoutes  | Worst Case   | 10000 | 5   | 7.241mb  | 1.267μs | ±2.35% |
| benchDynamicRoutes | Best Case    | 10000 | 5   | 7.241mb  | 2.007μs | ±1.64% |
| benchDynamicRoutes | Average Case | 10000 | 5   | 7.241mb  | 1.984μs | ±1.87% |
| benchDynamicRoutes | Worst Case   | 10000 | 5   | 7.241mb  | 1.929μs | ±1.69% |
+--------------------+--------------+-------+-----+----------+---------+--------+

PikoRouter
+--------------------+--------------+-------+-----+----------+---------+--------+
| subject            | set          | revs  | its | mem_peak | mode    | rstdev |
+--------------------+--------------+-------+-----+----------+---------+--------+
| benchStaticRoutes  | Best Case    | 10000 | 5   | 1.862mb  | 0.327μs | ±1.78% |
| benchStaticRoutes  | Average Case | 10000 | 5   | 1.862mb  | 0.330μs | ±2.37% |
| benchStaticRoutes  | Worst Case   | 10000 | 5   | 1.862mb  | 0.318μs | ±1.61% |
| benchDynamicRoutes | Best Case    | 10000 | 5   | 1.862mb  | 1.308μs | ±1.51% |
| benchDynamicRoutes | Average Case | 10000 | 5   | 1.862mb  | 1.800μs | ±0.72% |
| benchDynamicRoutes | Worst Case   | 10000 | 5   | 1.862mb  | 1.735μs | ±2.64% |
+--------------------+--------------+-------+-----+----------+---------+--------+

FastRoute
+--------------------+--------------+-------+-----+----------+----------+--------+
| subject            | set          | revs  | its | mem_peak | mode     | rstdev |
+--------------------+--------------+-------+-----+----------+----------+--------+
| benchStaticRoutes  | Best Case    | 10000 | 5   | 3.155mb  | 0.228μs  | ±2.46% |
| benchStaticRoutes  | Average Case | 10000 | 5   | 3.155mb  | 0.213μs  | ±1.19% |
| benchStaticRoutes  | Worst Case   | 10000 | 5   | 3.155mb  | 0.235μs  | ±1.60% |
| benchDynamicRoutes | Best Case    | 10000 | 5   | 3.155mb  | 0.652μs  | ±1.36% |
| benchDynamicRoutes | Average Case | 10000 | 5   | 3.155mb  | 12.908μs | ±1.13% |
| benchDynamicRoutes | Worst Case   | 10000 | 5   | 3.155mb  | 31.784μs | ±1.53% |
+--------------------+--------------+-------+-----+----------+----------+--------+

Routers match against 1000 routes

Benchmark against 5000 generated routes

ROUTES=5000 ./vendor/bin/phpbench run --revs=10000 --report='extends:aggregate,break:["benchmark"]'
SymfonyRouter
+--------------------+--------------+-------+-----+----------+---------+--------+
| subject            | set          | revs  | its | mem_peak | mode    | rstdev |
+--------------------+--------------+-------+-----+----------+---------+--------+
| benchStaticRoutes  | Best Case    | 10000 | 5   | 31.488mb | 3.911μs | ±2.47% |
| benchStaticRoutes  | Average Case | 10000 | 5   | 31.488mb | 3.709μs | ±1.78% |
| benchStaticRoutes  | Worst Case   | 10000 | 5   | 31.488mb | 3.771μs | ±1.40% |
| benchDynamicRoutes | Best Case    | 10000 | 5   | 31.488mb | 4.775μs | ±1.74% |
| benchDynamicRoutes | Average Case | 10000 | 5   | 31.488mb | 4.844μs | ±0.46% |
| benchDynamicRoutes | Worst Case   | 10000 | 5   | 31.488mb | 5.657μs | ±2.16% |
+--------------------+--------------+-------+-----+----------+---------+--------+

PikoRouter
+--------------------+--------------+-------+-----+----------+---------+--------+
| subject            | set          | revs  | its | mem_peak | mode    | rstdev |
+--------------------+--------------+-------+-----+----------+---------+--------+
| benchStaticRoutes  | Best Case    | 10000 | 5   | 4.569mb  | 0.312μs | ±1.82% |
| benchStaticRoutes  | Average Case | 10000 | 5   | 4.569mb  | 0.313μs | ±1.01% |
| benchStaticRoutes  | Worst Case   | 10000 | 5   | 4.569mb  | 0.313μs | ±0.40% |
| benchDynamicRoutes | Best Case    | 10000 | 5   | 4.569mb  | 1.242μs | ±1.92% |
| benchDynamicRoutes | Average Case | 10000 | 5   | 4.569mb  | 1.897μs | ±1.36% |
| benchDynamicRoutes | Worst Case   | 10000 | 5   | 4.569mb  | 1.947μs | ±2.04% |
+--------------------+--------------+-------+-----+----------+---------+--------+

FastRoute
+--------------------+--------------+-------+-----+----------+-----------+--------+
| subject            | set          | revs  | its | mem_peak | mode      | rstdev |
+--------------------+--------------+-------+-----+----------+-----------+--------+
| benchStaticRoutes  | Best Case    | 10000 | 5   | 11.248mb | 0.208μs   | ±1.44% |
| benchStaticRoutes  | Average Case | 10000 | 5   | 11.248mb | 0.211μs   | ±1.33% |
| benchStaticRoutes  | Worst Case   | 10000 | 5   | 11.248mb | 0.225μs   | ±1.51% |
| benchDynamicRoutes | Best Case    | 10000 | 5   | 11.248mb | 0.584μs   | ±1.96% |
| benchDynamicRoutes | Average Case | 10000 | 5   | 11.248mb | 85.164μs  | ±1.09% |
| benchDynamicRoutes | Worst Case   | 10000 | 5   | 11.248mb | 171.611μs | ±0.84% |
+--------------------+--------------+-------+-----+----------+-----------+--------+

Routers match against 5000 routes

You might also like...
A fast & flexible router

Klein.php klein.php is a fast & flexible router for PHP 5.3+ Flexible regular expression routing (inspired by Sinatra) A set of boilerplate methods fo

Toro is a PHP router for developing RESTful web applications and APIs.

Toro Toro is a PHP router for developing RESTful web applications and APIs. It is designed for minimalists who want to get work done. Quick Links Offi

Thruway - an open source client and router implementation of WAMP (Web Application Messaging Protocol), for PHP.

PHP Client and Router Library for Autobahn and WAMP (Web Application Messaging Protocol) for Real-Time Application Messaging

OpenAPI (Swagger) Specification Support for Sunrise Router (and not only)

OpenAPI (Swagger) Specification Support for Sunrise Router Important to understanding OpenAPI Specification Installation composer require 'sunrise/htt

A web router implementation for PHP.
A web router implementation for PHP.

Aura.Router Powerful, flexible web routing for PSR-7 requests. Installation and Autoloading This package is installable and PSR-4 autoloadable via Com

:bird: Simple PHP router

Macaw Macaw is a simple, open source PHP router. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to

A simple PHP Router

Panda Router Description the panda-router is a small alternative PHP router that can be used for small projects. With this router you can use differen

A PHP Router Package

Router A PHP Router Package Basic Concepts A router package is a utility that, once all http requests are redirected to an entry point, can configure

The simple PHP router

Macaw Macaw is a simple, open source PHP router. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to

Comments
  • chore: general improvements

    chore: general improvements

    This commit adds a .editorconfig file (https://editorconfig.org/).

    It also improves README.md, renames LICENCE.txt to LICENSE.txt, and changes composer.json so new packages are sorted by name.

    PS.: I know this is a small contribution, but could you please add a hacktoberfest-accepted label on this PR or add a hacktoberfest label on the main repo? I really appreciate it!

    hacktoberfest-accepted 
    opened by chapeupreto 1
Owner
Piko framework
PHP micro MVC framework codebase
Piko framework
PHP Router class - A simple Rails inspired PHP router class.

PHP Router class A simple Rails inspired PHP router class. Usage of different HTTP Methods REST / Resourceful routing Reversed routing using named rou

Danny van Kooten 565 Jan 8, 2023
A lightweight and simple object oriented PHP Router

bramus/router A lightweight and simple object oriented PHP Router. Built by Bram(us) Van Damme (https://www.bram.us) and Contributors Features Support

Bramus! 935 Jan 1, 2023
A lightweight and very basic PHP router.

Katya A lightweight PHP router Configuration Para servidor Apache, en el directorio del proyecto crea y edita un archivo .htaccess con lo siguiente: <

Luis Rodríguez 0 Apr 4, 2022
:tada: Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)

HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger) Installation compo

Sunrise // PHP 151 Jan 5, 2023
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project.

Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.

Simon Sessingø 472 Jan 4, 2023
Pux is a fast PHP Router and includes out-of-box controller tools

Pux Pux is a faster PHP router, it also includes out-of-box controller helpers. 2.0.x Branch Build Status (This branch is under development) Benchmark

Yo-An Lin 1.3k Dec 21, 2022
PHPRouter is an easy-to-use, fast, and flexible PHP router package with express-style routing.

PHP-Router is a modern, fast, and adaptable composer package that provides express-style routing in PHP without a framework.

Ayodeji O. 4 Oct 20, 2022
klein.php is a fast & flexible router for PHP 5.3+

Klein.php klein.php is a fast & flexible router for PHP 5.3+ Flexible regular expression routing (inspired by Sinatra) A set of boilerplate methods fo

null 2.6k Jan 7, 2023
Fast request router for PHP

FastRoute - Fast request router for PHP This library provides a fast implementation of a regular expression based router. Blog post explaining how the

Nikita Popov 4.7k Dec 23, 2022
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.

The PHP HTTP Flight Router divineniiquaye/flight-routing is a HTTP router for PHP 7.1+ based on PSR-7 and PSR-15 with support for annotations, created

Divine Niiquaye Ibok 16 Nov 1, 2022