PHP code example of sunnyflail / router

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

    

sunnyflail / router example snippets


    use SunnyFlail\Router\Router;
    $router = new Router();
    $router->insertConfig([
        [
            "index",
            "/",
            fn() => print("Talking to you from '/'!"),
        ] 
    ]);

    use SunnyFlail\Router\Route;
    $routeOne = new Route("index", "/", fn() => print("Hello world, from index!"));
    $routeTwo = new Route(
        "offer", "/offer/{id}",
        fn($id) => printf("Hello world, from offer number %s!", $id),
        ["GET", "HEAD"],
        ["id" => "\d+"],
        ["id" => 1]
    );
    $router->addRoutes($routeOne, $routeTwo);

    $router->addRoute("login", "/login/", [LoginController::class, "login"]);

    $matchedData = $router->match($_SERVER["REQUEST_METHOD"], $_SERVER["REQUEST_PATH"]);

    $route = $router->getRoute("offer");

    $url = $route->generateUrl(["id" => 2]);