PHP code example of codeinc / router

1. Go to this page and download the library: Download codeinc/router library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codeinc / router example snippets



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'


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