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
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).