PHP code example of felipecwb / routing

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

    

felipecwb / routing example snippets




use Felipecwb\Routing\Router;
// Exceptions
use Felipecwb\Routing\Exception\ResolverException;
use Felipecwb\Routing\Exception\RouteNotFoundException;

$router = Router::create();

$router->add('/', function () {
    echo "Hello World!";
});

$router->add('/hello/(\w+)', function ($name) {
    echo "Hello {$name}!";
});

$router->add('/article/(\d+)', function ($id, $extraStr) {
    echo "Article {$id}! ${extraStr}";
});

try {
    $router->dispatch('/');
    // with arguments
    $router->dispatch('/hello/felipecwb');
    // with extra arguments
    $router->dispatch('/hello/10', ['Extra String!']);
} catch (RouteNotFoundException $e) {
    echo "Sorry! The target can not be found!";
} catch (ResolverException $e) {
    echo "Sorry! The target can not be executed!";
}

die;