PHP code example of hypario / router

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

    

hypario / router example snippets


$router = new Hypario\Router(); // Here no parameters needed
$router->get('/', function () { echo "Hello World"; }); // Define a route in GET method.

$router = new Hypario\Router();
$router->post('/', function () { echo "Route accessed via POST method"; });

$router = new Hypario\Router();
$router->get('/', function () { echo "Hello World"; });

$route = $router->match($_SERVER['REQUEST_URI']);

$router = new Hypario\Router();
$router->get('/', function () { echo "Hello World"; });

$route = $router->match($_SERVER['REQUEST_URI']); // We get the matched route

if (!is_null($route)) {
    $function = $route->getHandler(); // We get the function
    call_user_func($function); // We call the function of the matched route
}

$router->get('/hello/{name:[a-z]+}', function($name) { echo "Hello $name";});

$route = $router->match($_SERVER['REQUEST_URI']);

if (!is_null($route)) {
    $function = $route->getHandler();
    call_user_func_array($function, $route->getParams());
}

$router->get('/', 'handler', 'index'); # creates a route called index

$pathToIndex = $router->getPath('index');

$router->get('/articles/{id:[0-9]+}', 'handler', 'article');

$pathToArticle = $router->getPath('article', ["id" => 1]); 
// returns /articles/1

$router->get('/', 'handler', 'index');
$router->get('/a', 'handler', 'index');

$pathToIndex = $router->getPath('index');
// returns /a