PHP code example of pollen-solutions / routing

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

    

pollen-solutions / routing example snippets


 declare(strict_types=1);

use Pollen\Http\Request;
use Pollen\Http\Response;
use Pollen\Http\ResponseInterface;
use Pollen\Routing\Router;

// Router instantiation
$router = new Router();

// Map a route
$router->map('GET', '/', static function (): ResponseInterface {
    return new Response('<h1>Hello, World!</h1>');
});

$router->map('GET', '/phpinfo', static function () {
    ob_start();
    phpinfo();
    return new Response(ob_get_clean());
});

// Setting Handle Request (optional)
$psrRequest = Request::createFromGlobals()->psr();

// Map a Fallback Route (optional)
$router->setFallback(function () {
    return new Response('<h1>404 - Page not found !</h1>', 404);
});

// Catch HTTP Response
$response = $router->handle($psrRequest);

// Send the response to the browser
$router->send($response);

// Trigger the terminate event
$router->terminate($psrRequest, $response);