PHPackages                             zeroframe/zerorouter - 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. [Framework](/categories/framework)
4. /
5. zeroframe/zerorouter

ActiveLibrary[Framework](/categories/framework)

zeroframe/zerorouter
====================

PHP light and very fast router based on PSR-7 and able to mix different strategies.

0.0.1(7y ago)111BSD-3-ClausePHP

Since Sep 14Pushed 7y agoCompare

[ Source](https://github.com/nsn0x01/zerorouter)[ Packagist](https://packagist.org/packages/zeroframe/zerorouter)[ RSS](/packages/zeroframe-zerorouter/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

PHP ZeroRouter
==============

[](#php-zerorouter)

It's a **light and very fast router** able to mix different strategies and you can also implement your own.
The router is based on **PSR-7** and we suggest use a fast implementation like nyholm/psr7 and nyholm/psr7-server.
You can use this Router in your **Middleware** or as standalone component.

The main target is to give a linear complexity to the problem and strategies like **PrefixBlockLookupStrategy** and **SuffixBlockLookupStrategy**works well for reaching the target.

Below each strategy is explained.

HeaderKeyLookupStrategy
=======================

[](#headerkeylookupstrategy)

Skip all strategies and use the header key X-Routing-Key for matching directly your route and get best performance. Could be useful with microservices and large dynamic maps.

Support only dynamic routes.

PrefixBlockLookupStrategy
=========================

[](#prefixblocklookupstrategy)

This strategy splits the url by blocks and each block is delimited by a '/'. We create a map only with static parts before the dynamic route.

Example:

Route: /comments/sport/{id}

map: \[ 'comments' =&gt; \[ 'sport' =&gt; \[ 'regsex' =&gt; \[...\] \] \] \]

The strategy will try lookup each block directly in array map, if a block it's not found then we try check on the last block found if exists a node "regsex" and then we try to match the regular expressions. With this strategy you can have large dynamic maps without having bad performance because the access to the array is very fast and at every access you are limiting the data to few regular expressions.

Following the previous example with this URL: /comments/sport/8041984 PrefixBlockLookupStrategy will do these steps:

- accessing to array using 'comments' key \[ OK \]
- accessing to array using 'sport' key \[ OK \]
- accessing to array using '8041984' key \[ FAILS \]
- trying to match the regular expressions on last block found, in this case our block is 'sport'.

handles URLS where the static part is before the dynamic route:

/users/{id} /users/comments/{id} /profiles/{activity}/id

SuffixBlockLookupStrategy
=========================

[](#suffixblocklookupstrategy)

Apply same strategy used by PrefixBlockLookupStrategy but in reverse order working only on suffixes.

handles URLS where the static part is after the dynamic route:

/{id}/users /{id}/comments/users /{id}/{activity}/profiles

StaticKeyLookupStrategy
=======================

[](#statickeylookupstrategy)

Basic and best strategy for static routes.

/dummy
/php
/dave

GenericBlockRegexLookupStrategy
===============================

[](#genericblockregexlookupstrategy)

This strategy tries to match only generic regular expression, a generic regex contains only dynamic routes without static part in the url.

Install with composer
=====================

[](#install-with-composer)

```
$ composer require zeroframe/zerorouter
```

basic usage:
============

[](#basic-usage)

```
