PHP code example of kyleblanker / php-router

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

    

kyleblanker / php-router example snippets


$router = new \KyleBlanker\Router();

$router = new \KyleBlanker\Router();

$router->route('GET','/my-route',function(){
    echo 'This is my route';
});


$router = new \KyleBlanker\Router();

$router->route('GET','/my-route/{variable}',function($variable){
    echo 'This is my route variable ' . $variable;
});

$router = new \KyleBlanker\Router();

$router->group('/my-group', function($router){
    $router->route('GET','/my-route',function(){
        echo 'This is my route';
    });
});


$router = new \KyleBlanker\Router();

$router->route('GET','/my-route/{id:/[^0-9]/}',function($id){
    echo 'This is my route';
});


$router = new \KyleBlanker\Router();

$router->get('/get-route',function(){
    echo 'This is my get route';
});

$router->post('/post-route',function(){
    echo 'This is my post route';
});

$router->any('/any-route',function(){
    echo 'This is my any route';
});

$router = new \KyleBlanker\Router();

$router->route('GET','/my-route/{id:/[^0-9]/}',function($id){
    echo 'This is my route';
});

try
{
    $response = $router->dispatch($_SERVER['REQUEST_METHOD'],$_SERVER['REQUEST_URI']);
}
catch(\KyleBlanker\Routing\Exceptions\RouteNotFoundException $e)
{
    // A route was not found
}
catch(\KyleBlanker\Routing\Exceptions\MethodNotAllowedException $e)
{
    // A route was found, but the http method was not supported.
}

$route_handler = $response[Router::ROUTE_HANDLE];
$route_parameters = $response[Router::ROUTE_PARAMETERS];