PHP code example of ajgarlag / psr15-router

1. Go to this page and download the library: Download ajgarlag/psr15-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/ */

    

ajgarlag / psr15-router example snippets


use Ajgarlag\Psr15\Router\Matcher\UriRegexRequestMatcher;
use Ajgarlag\Psr15\Router\Middleware\Route;
use Ajgarlag\Psr15\Router\Middleware\ArrayRouter;
use Ajgarlag\Psr15\Router\Middleware\RouterMiddleware;

$userMiddleware; //Some middleware to process user requests
$userRoute = new Route(
    new UriRegexRequestMatcher('^http(s)?://example.org/user/'),
    $userMiddleware
);
$adminMiddleware; //Some middleware to process admin requests
$adminRoute = new Route(
    new UriRegexRequestMatcher('^http(s)?://example.org/admin/'),
    $adminMiddleware
);

$router = new ArrayRouter();
$router->addRoute($userRoute);
$router->addRoute($adminRoute);

$routerMiddleware = new RouterMiddleware($router);

$response = $routerMiddleware->process($request, $requestHandler);

use Ajgarlag\Psr15\Router\Matcher\UriRegexRequestMatcher;
use Ajgarlag\Psr15\Router\RequestHandler\Route;
use Ajgarlag\Psr15\Router\RequestHandler\ArrayRouter;
use Ajgarlag\Psr15\Router\RequestHandler\RouterRequestHandler;

$userRequestHandler; //Some request handler to process user requests
$userRoute = new Route(
    new UriRegexRequestMatcher('^http(s)?://example.org/user/'),
    $userRequestHandler
);
$adminRequestHandler; //Some request handler to process admin requests
$adminRoute = new Route(
    new UriRegexRequestMatcher('^http(s)?://example.org/admin/'),
    $adminRequestHandler
);

$router = new ArrayRouter();
$router->addRoute($userRoute);
$router->addRoute($adminRoute);

$failoverRequestHandler; // Request handler that returns 404 unconditionally
$routerRequestHandler = new RouterRequestHandler($router, $failoverRequestHandler);

$response = $routerRequestHandler->handle($request);