Download the PHP package fastero/php-router without Composer
On this page you can find all versions of the php package fastero/php-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fastero/php-router
More information about fastero/php-router
Files in fastero/php-router
Package php-router
Short Description Simple and fast php router
License MIT
Homepage https://github.com/lehaBay/php-router
Informations about the package php-router
PHP Router
Simple, fast and fully covered with a tests Router.
Router is a very important part of (almost)any web application and as a part of core it must have as less overhead as possible. That's what this router is built for -- to be extremely fast
requires php 7.0 +
Install with composer
Usage
array contain's route names as keys and configuration parameters array as value. It's recommended to generate configuration using Configurators
And to use reverse routing (path generation):
See Generate path
Configurators
Configurators are created to make configuration of your route easier and prevent you from making mistakes.
While configurators are using chaining syntax they are not return an object as a result but simple array and they don't create a bunch of objects while being used. This method has some limitations but it has much less overhead and almost as fast as using simple arrays and speed is a main goal of this library.
To start configuring route you must call static method on appropriate Configurator. This method usually has two parameters and . Example:
now is a Object and other it's methods can be called to continue configuration and all of theme will return object and at the very end you must call method which will return array with all the configurations. So at the end it looks like:
All the configurators has following methods:
Add a default value for parameter and/or validation rule. Validation rule is a regular expression.
This controller will be returned as a part of route options so you can call it or do whatever you want. It has no special meaning for Router class
Some route handlers may not be able to generate reverse path from path by itself (for example Regex route) so you need to specify this pattern and reverse path generator as well (see next)
Class name that will be used as a generator with specified reversePattern. If specified it will be used instead of built in route handler generator.
Set rules for query parameter
Currently there are 3 configurators (src/Configuration) one per each route type:
-
- corresponding the \Fastero\Router\PathHandler\Literal route type
-
- corresponding the \Fastero\Router\PathHandler\Section route type
-
- corresponding the \Fastero\Router\PathHandler\Regex route type
All they have the same methods but slightly different signature for ::config() method. That's because Literal route type does not need any path pattern but static path only
Route types
Literal
- Represents simple static path that has no parameters like "admin/users", "article/list" etc. It's fastest route type and should be used whenever possible
Regex
- Represents Regular Expression route type. It's requires a regular expression (in the format that preg_match can execute without delimiters and "^"""$" operators as they included automatically ) with named parameters e.g.: .
It's recommended to make regex as simple as possible and use validators if you need to validate parameters rather than include all the validation in the regex.
Section
-
- This is route in format like
and here is a static prefix or just - more unique prefix is better but sometimes it's more important to be looking good and readable.
so this type of route uses "/" to delimit parameters and ":" to define parameter "[", "]" - to define optional parameter.
More examples (with static prefixes as they should be used. You may think about if like it's a single concat string):
- tag - is required parameter
- author_name - is optional this will match following paths: 'news/politics', 'news/local/author/Grou'
Generate path
Currently there are two path generators:
-
- which is also a path handler. This generator will simply return path.
-
- it uses the same format as a \Fastero\Router\PathHandler\SectionPathMatcher and is a default generator for this matcher.
Format:
- [] - optional section, will be generated only if any parameter inside is set
- :id - parameter name - will be replaced with actual parameter if given. [a-zA-Z0-9] characters are allowed, started with [a-zA-Z]
- characters '[', ']', ':' - can be escaped with '\' if meant as literals
Examples:
-
- "id" is required parameter
-
-
'author' and 'year', 'month', 'day' are optional. Results: , ,
-
Why is it fast?
There are two factors - first it uses native array as a configuration and does not create an object for every route. Even though creating a few objects per route is not a big deal it will still some part of your performance and this part will be bigger more routes you have.
Second factor - is that it forces you to use static prefixes to your paths. And searching by static prefixes first is much faster than executing each regex. Some other libraries are doing the same but they trying to figure out prefixes by itself and in order for this to be efficient you would need use cache. While using cache is not a bad thing to do usually you want your app to be fast enough without cache and then make it even faster with cache.
MIT Licensed, http://www.opensource.org/licenses/MIT