PHP code example of il4mb / routing

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

    

il4mb / routing example snippets


use Il4mb\Routing\Http\Method;
use Il4mb\Routing\Map\Route;

class AdminController
{
    #[Route(Method::GET, "/home")]
    public function home()
    {
        return "Welcome to the Admin Home!";
    }
}

namespace App\Middlewares;

use Il4mb\Routing\Http\Request;
use Il4mb\Routing\Http\Response;
use Closure;

class AuthMiddleware implements Middleware
{
    public function handle(Request $request, Closure $next): Response
    {
        // do something 

        return $next($request);
    }
}

#[Route(Method::GET, "/dashboard", middlewares: [AuthMiddleware::class])]
public function dashboard()
{
    return "Welcome to the dashboard!";
}

use Il4mb\Routing\Middlewares\MiddlewareExecutor;
use Il4mb\Routing\Http\Request;

$executor = new MiddlewareExecutor([
    App\Middlewares\AuthMiddleware::class
]);

$request = new Request();
$response = $executor($request, function ($req) {
    return new Response("Request handled successfully.");
});

echo $response->getBody();