PHP code example of erlendellingsen / flex-router

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

    

erlendellingsen / flex-router example snippets




// Load the namespaces
use FlexRouter\FlexRouter;
use FlexRouter\Utilities\FlexResolver;

// Load in composer's autoloader
Route           | Route Id                           */
$router->registerRoute('GET',           '/',              'homepage'); // Can pass single method
$router->registerRoute(['GET', 'POST'], '/test',          'test');     // Or an array of methods
$router->registerRoute('GET',           '/test/:id/post', 'param');    // You can create URL params
$router->registerRoute('GET',           '/asdf*',         'wildcard'); // Or Wildcard Params

// Create your resolver
$requestMethod = $_SERVER['REQUEST_METHOD']; // Can be attained from either globals or request object
$requestUri    = $_SERVER['REQUEST_URI'];    // Can be attained from either globals or request object
$resolver      = new FlexResolver($requestMethod, $requestUri, $router);

// Start resolving your routes in whatever way you would like,
if ($resolver->resolve('homepage')) {
    echo 'The homepage route was found';

    return;
}

if ($resolver->resolve('test')) {
    echo 'The nested test route was found';

    return;
}

if ($resolver->resolve('param')) {
    echo "The param test route was found\r\n";
    echo $resolver->access()->params('url', 'id');

    return;
}

if ($resolver->resolve('wildcard')) {
    echo 'The wildcard test route was found';

    return;
}

// This is the 404 catch all
$resolver->notFound(function () {
    echo 'Nothing found';

    return;
});
htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ index.php?path=$1 [NC,L,QSA]