PHP code example of asko / router

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

    

asko / router example snippets


use Asko\Router\Router;

$router = new Router();

$router->get("/hello/{who}", function(string $who) {
    echo "Hello, {$who}";
});

$router->dispatch();

use Asko\Router\Router;

class AdminController
{
    public function login()
    {
        echo "Login page goes here.";
    }
}

$router = new Router();
$router->get("/admin/login", [AdminController::class, "login"]);
$router->dispatch();

use Asko\Router\Router;

function hello_world() {
    echo "Hello, World!";
}

$router = new Router();
$router->get("/hello-world", "hello_world");
$router->dispatch();

use Asko\Router\Router;

$router = new Router();

$router->get("/hello-world", function() {
    echo "Hello, World!";
});

$router->dispatch();

$router->get("/hello/{who}", ...);

$router->get("/hello/{who}", function(string $who) {
    echo "Hello, {$who}!";
});

use Asko\Router\Router;

class SomeDependency {}

$router = new Router();

$router->get("/hello/{who}", function(SomeDependency $dep, string $who) {
    echo "Hello, {$who}";
});

$router->dispatch();

use Asko\Router\Router;

class SomeMiddleware
{
    public function handle(string $who): string
    {
        return "intercepted, {$who}!";
    }
}

$router = new Router();

$router->get(
    path: '/hello/{who}', 
    callable: fn(string $who) => "Hello, {$who}!",
    middlewares: [SomeMiddleware::class]
});