PHP code example of krubio / perfect-router

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

    

krubio / perfect-router example snippets


 declare(strict_types=1);

iner;
use PerfectApp\Logger\FileLogger;
use PerfectApp\Routing\Router;

$logger = new FileLogger('errors.log');

$container = new Container();

$router = new Router($container);
$router->autoRegisterControllers(__DIR__ . '/src/Controllers');

// A user-defined exception handler function
$router->setNotFoundHandler(function ($requestUri, $requestMethod) use ($logger) {
    $logger->error("Route $requestUri with method $requestMethod not found.");
    http_response_code(404);
    echo "Route $requestUri with method $requestMethod not found.";
});

try {
    $router->dispatch($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
} catch (RuntimeException $e) {
    $logger->error($e->getMessage());
    http_response_code(404);
    echo "Route not found.";
}