PHP code example of ellipse / router-aura

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

    

ellipse / router-aura example snippets




namespace App;

use Aura\Router\RouterContainer;

use Ellipse\Router\AuraRouterRequestHandler;

// Get a psr7 request.
$request = some_psr7_request_factory();

// Create an aura router.
$router = new RouterContainer;

// Set the router map builder.
$router->setMapBuilder(function ($map) {

    // The route handlers must be Psr-15 request handlers.
    $map->get('index', '/', new SomeRequestHandler);

    // When this route is matched a new request with an 'id' attribute would be passed to the request handler.
    $map->get('path', '/path/{id}', new SomeOtherRequestHandler);

});

// Create an aura router request handler using this aura router.
$handler = new AuraRouterRequestHandler($router);

// Produce a response with the aura router request handler.
$response = $handler->handle($request);



namespace App;

use Aura\Router\RouterContainer;

use Ellipse\Router\AuraRouterMiddleware;

// Get a psr7 request.
$request = some_psr7_request_factory();

// Create an aura router.
$router = new RouterContainer;

// Set the router map builder.
$router->setMapBuilder(function ($map) {

    // The route handlers must be Psr-15 request handlers.
    $map->get('index', '/', new SomeRequestHandler);

    // When this route is matched a new request with an 'id' attribute would be passed to the request handler.
    $map->get('path', '/path/{id}', new SomeOtherRequestHandler);

});

// Create an aura router middleware using this aura router.
$middleware = new AuraRouterMiddleware($router);

// When a route is matched the request is handled by the matched request handler.
// Otherwise NextRequestHandler is used to handle the request.
$response = $middleware->process($request, new NextRequestHandler);