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

ActiveLibrary[API Development](/categories/api)

vertilia/router
===============

Highly efficient multi-purpose router library

v2.1.0(2y ago)021BSD-2-ClausePHPPHP &gt;=7.4CI failing

Since May 14Pushed 2y ago1 watchersCompare

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

READMEChangelog (3)Dependencies (4)Versions (23)Used By (0)

router
======

[](#router)

A lightweight http routing library tuned for performance, allowing translation of request path into controller name and validation of path parameters as defined in [OpenAPI specification](http://spec.openapis.org/oas/v3.0.2#patterned-fields) and (partly) in [URI Template](https://www.rfc-editor.org/rfc/rfc6570).

A highly efficient routing mechanism is especially useful in large routing tables since the number of operations to find the controller depends on the routing tree levels and not on the total number of routes in the list.

Simply saying, instead of looking up all available paths represented as a list of regular expressions, we construct a routing tree of folders from this list of paths and only visit the levels corresponding to current request. This leads us step by step to the correct route instead of blindly scanning the whole list of available routes until the matching route is found (or not). Also, the time to find incorrect routes is minimized, we don't need to scan the whole list to find out that a route is missing.

People regularly researching their access logs to discover the most frequent requests to put them higher in routes list will appreciate the functionality. These researches are now a history, and they may put their precious time on something more important.

The router algorithm is universal, in a sense that when defining routing rules, you can attach any structure to the leaf node, and it will be returned when corresponding route is provided. This leaf node is represented by an array containing at least `controller` element with the name of operation that will be launched by the user-land code when the route is found. If requested path contained parameters, they will be detected and injected in returned array as `parameters`element.

Since we believe in high effectiveness of [`ValidArray`](https://github.com/vertilia/valid-array)-based `HttpRequestInterface` structure in context of request handling, we also provide an `HttpRequestRouter` class allowing automatic registration of route parameters in request object, their validation via filters provided in leaf elements and easy access via array notation as in `$request['param']`. These parameters are guaranteed to be valid following the user-specified format (see tests).

High efficiency of filtering mechanism is backed by php-native [`filter` extension](https://php.net/filter).

Usage
=====

[](#usage)

Universal router
----------------

[](#universal-router)

When instantiating a universal router you need to pass the [`HttpParserInterface`](https://github.com/vertilia/parser)parser object (which will translate the parameters placeholders to regexps) and a list of routing files.

```
