PHP code example of karmabunny / router

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

    

karmabunny / router example snippets


use karmabunny\router\Router;

// Create a router with a config.
$router = Router::create([]);

// Load some routes.
// These are keyed:
// [ rule => target ]
$routes =  in the route table.
echo $action->target, PHP_EOL;

// The path arguments. Keyed by name. Wildcards are always last.
echo $action->args, PHP_EOL;

// Assert the target is callable:
// either a plain method, closure, or [class, method].
$action->isCallable();

// Assert the target is in the form of [class, method].
if ($action->isController(MyController::class)) {
    $controller = $action->createController();
    $result = $action->invoke($controller);
}
else {
    // For everything else.
    $result = $action->invoke();
}

use karmabunny/route/Route;

class MyController
{
    #[Route('GET /my/php8/{route}')]
    public function php8Action() {}

    /** @route GET /my/php7/{route} */
    public function php7Action() {}
}