2017 © Pedro PelĂĄez
 

library lib-router

Code Inc. PSR7 & PSR15 router library

image

codeinchq/lib-router

Code Inc. PSR7 & PSR15 router library

  • Tuesday, February 27, 2018
  • by joanfabregat
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 22 Versions
  • 0 % Grown

The README.md

PSR-7 & PSR-15 router library

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)., (*1)

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., (*2)

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:, (*3)

composer require codeinc/router

License

This library is published under the MIT license (see the LICENSE file)., (*4)

The Versions

15/12 2017

1.0.11

1.0.11.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

MIT

The Requires

 

12/12 2017

1.0.10

1.0.10.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

MIT

The Requires

 

12/12 2017

1.0.9

1.0.9.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

11/12 2017

1.0.8

1.0.8.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

11/12 2017

1.0.7

1.0.7.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

11/12 2017

1.0.6

1.0.6.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

11/12 2017

1.0.5

1.0.5.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

11/12 2017

1.0.4

1.0.4.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

11/12 2017

1.0.3

1.0.3.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

11/12 2017

1.0.2

1.0.2.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

09/12 2017

1.0.1

1.0.1.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

09/12 2017

1.0.0

1.0.0.0 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

06/12 2017

1.0.0-beta.3

1.0.0.0-beta3 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

06/12 2017

1.0.0-beta.2

1.0.0.0-beta2 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires

 

04/12 2017

1.0.0-beta.1

1.0.0.0-beta1 https://github.com/codeinchq/lib-gui

Code Inc.'s GUI library

  Sources   Download

proprietary

The Requires