codeinc / router
Code Inc. PSR7 & PSR15 router library
Requires
- php: >=7.1
- codeinc/psr7-responses: ^2
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- codeinc/error-renderer: ^1.2
- codeinc/psr7-response-sender: ^1.1
- guzzlehttp/psr7: ^1.4
- phpunit/phpunit: ^7
Suggests
- codeinc/middleware-dispatcher: A PSR-15 middleware dispatcher
- codeinc/psr7-response-sender: A PSR-7 response sender
- codeinc/psr7-responses: Provides a collection of PSR-7 responses
- codeinc/router-annotation-resolver: A resolver using Doctrine's annotation system to detect and configure request handlers
- codeinc/router-routable-resolver: A resolver using interfaces to detect and configure request handlers
- dev-master
- 4.x-dev
- 4.0.0
- 3.x-dev
- 3.4.1
- 3.4.0
- 3.4.0-beta.2
- 3.4.0-beta.1
- 3.3.0
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.x-dev
- 2.3.1
- 2.3.1-beta.1
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 2.0.0-beta.5
- 2.0.0-beta.4
- 2.0.0-beta.3
- 2.0.0-beta.2
- 2.0.0-beta.1
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.12
- 1.3.11
- 1.3.10
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0-beta.1
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-beta.3
- 1.0.0-beta.2
- 1.0.0-beta.1
This package is auto-updated.
Last update: 2020-02-07 20:53:57 UTC
README
This library provides a PSR-7 and PSR-15 compatible router. The router is a component in charge or selecting the appropriate controller (implementing ControllerInterface
). It behaves as a PSR-15 middleware (MiddlewareInterface
).
Resolvers
In order to select a controller, the router relies on a resolver (implementing ResolverInterface
). Resolvers are in charge of matching a URI path (called a route) to a controller and vice versa, mapping a controller to a route. Two resolvers are provided, StaticResolver
which uses a list of preset patterns (shell patterns resolved by fnmatch()
) matching handler's classes and DynamicResolver
which dynamically calculates the routes of namespaces base on a PHP namespace and a base URI. You can pair multiple resolvers using ResolverAggregator
. External packages provides resolvers based on Doctrine's annotations or on interfaces.
Usage
Basic usage
<?php use CodeInc\Router\Router; use CodeInc\Router\ControllerInterface; use CodeInc\Router\Resolvers\StaticResolver; // dummy classes final class MyInstantiator implements ControllerInterface {} final class HomePage implements ControllerInterface {} final class License implements ControllerInterface {} final class Article implements ControllerInterface {} // instantiating the router $myRouter = new Router( new StaticResolver([ '/' => HomePage::class, '/license.txt' => License::class, '/article-[0-9]/*' => Article::class ]) ); // controller lookup (assuming the URI of the request is "/article-2456/a-great-article.html") $myRouter->process($aPsr7ServerRequest, $aFallbackHandler); // <-- returns 'ArticleController'
Working with multiple resolvers and multiple instantiators
<?php use CodeInc\Router\Router; use CodeInc\Router\ControllerInterface; use Doctrine\Instantiator\InstantiatorInterface; use CodeInc\Router\Resolvers\ResolverAggregator; use CodeInc\Router\Resolvers\StaticResolver; use CodeInc\Router\Resolvers\DynamicResolver; // dummy classes final class MyFirstInstantiator implements InstantiatorInterface {} final class MySecondInstantiator implements InstantiatorInterface {} final class HomePage implements ControllerInterface {} final class License implements ControllerInterface {} final class Article implements ControllerInterface {} // instantiating the router $myRouter = new Router( new ResolverAggregator([ new StaticResolver([ '/' => HomePage::class, '/license.txt' => License::class, '/article-[0-9]/*' => Article::class ]), new DynamicResolver( 'MyApp\\MyHandlers', // <-- handlers base namespace '/my-app/' // <-- handlers base URI ) ]) ); // processing the response $myRouter->process($aPsr7ServerRequest, $aFallbackHandler);
Installation
This library is available through Packagist and can be installed using Composer:
composer require codeinc/router
License
This library is published under the MIT license (see the LICENSE
file).