2017 © Pedro Peláez
 

library net-http-router

Web router implementation for PSR-7 requests

image

binsoul/net-http-router

Web router implementation for PSR-7 requests

  • Wednesday, April 27, 2016
  • by binsoul
  • Repository
  • 1 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

net-http-router

Latest Version on Packagist ![Software License][ico-license] Total Downloads, (*1)

This package provides a web router implementation for PSR-7 requests. It uses matchers to extract information from the given request and returns a route object., (*2)

Your application can use the information provided by the matching route to dispatch it. A dispatcher is not provided by this package., (*3)

Install

Via composer:, (*4)

``` bash $ composer require binsoul/net-http-router, (*5)


## Matcher A matcher can be a closure, an object with an __invoke method or an object implementing the Matcher interface. If a matcher decides that the route was found it can optionally set an response: ``` php $router->addMatcher( function (Route $route) { if ($route->getMissingPath() == '/hello') { $route->found(new Response('Hello world!')); } } ); $route = $router->match($request); // http:/domain/hello if ($route->hasResponse()) { echo $route->getResponse(); // Hello world! }

StaticMatcher

The StaticMatcher matches simple paths and sets the provided route parameters of a match., (*6)

The following example: ``` php $router->addMatcher( new StaticMatcher( [ '/blog' => ['responder' => 'Blog'] ] ) );, (*7)

$route = $router->match($request); // http://domain/blog var_export($route->getData());, (*8)

would output:
``` text
array('responder' => 'Blog',)

RegexMatcher

The RegexMatcher matches arbitrary regular expressions. Named capture group are set as parameters of the route., (*9)

The following example: ``` php $router->addMatcher( new RegexMatcher( [ '/edit/(?[0-9]+)' => ['responder' => 'Edit'] ] ) );, (*10)

$route = $router->match($request); // http://domain/edit/1 var_export($route->getData());, (*11)

would output:
``` text
array('id' => 1, 'responder' => 'Edit',)

ParameterMatcher

The ParameterMatcher matches paths with parameter placeholders., (*12)

Placeholders can be defined in the following format: ``` text [prefix+][name][=format[(length)]][?], (*13)


- Prefix: Any number of characters except "]" and "+" followed by a single "+". - Name: A single character followed by characters, numbers or "_". - Format: "=" followed by a defined format name optionally followed a length definition. - Length: A single number or a number range enclosed in parenthesis. - Marker: If the definition ends with a single "?" the parameter is optional. For example the path: ``` text /[year=number(4)][/+month=number(1-2)?]/[name].html

would match: ``` text /2015/09/article.html /2015/9/article.html /2015/article.html, (*14)


Found placeholders are set as parameters of the route. The following example: ``` php $router->addMatcher( new ParameterMatcher( [ '/[year]/[month]/[name].html' => ['responder' => 'Blog'] ] ) ); $route = $router->match($request); // http://domain/2015/09/article.html var_export($route->getData());

would output: ``` text array('year' => '2015', 'month' => '09', 'name' => 'article', 'responder' => 'Blog',), (*15)


### NamespaceMatcher The NamespaceMatcher allows to group other matchers under a common path prefix: The following definition: ``` php $matcher = new NamespaceMatcher( '/admin', [ new StaticMatcher( [ '/' => ['responder' => 'Home'], '/list' => ['responder' => 'List'], ] ), new RegexMatcher( [ '/edit/(?<id>[0-9]+)' => ['responder' => 'Edit'], ] ), ] );

would match: ``` text /admin/ /admin/list /admin/edit/1, (*16)


## Router Matchers can be registered either as closures or concrete objects or as strings. If a matcher is registered as a string the provided factory is used to lazily build the matcher object. ``` php $router->addMatcher('AccountMatcher'); $router->setFactory($factory); // will call $factory->buildMatcher('AccountMatcher'); $router->match($request);

Matchers are added to an internal queue which will be processed by the default router in the order they were added to the queue., (*17)

For example if the queue is build like: ``` php $router->addMatcher('Foo'); // matches /foo $router->addMatcher('Bar'); // matches /bar $router->addMatcher('Baz'); // matches /baz, (*18)

$router->match($request); // http://domain/foo/bar/baz, (*19)


The route object will change like this: ``` text Foo is called with missing path = '/foo/bar/baz' and matched path = '' Bar is called with missing path = '/bar/baz' and matched path = '/foo' Baz is called with missing path = '/baz' and matched path = '/foo/bar'

Testing

bash $ composer test, (*20)

License

The MIT License (MIT). Please see License File for more information., (*21)

The Versions

27/04 2016

dev-master

9999999-dev https://github.com/binsoul/net-http-router

Web router implementation for PSR-7 requests

  Sources   Download

MIT

The Requires

 

The Development Requires

http router network