1. Go to this page and download the library: Download vilnis/easy-route 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/ */
vilnis / easy-route example snippets
use EasyRoute\Router;
use EasyRoute\Exceptions\RouteNotFoundException;
use EasyRoute\Exceptions\MethodNotAllowedException;
use MyCustomNamespace\MyCustomController; // Replace with your custom namespace
// Include Composer's autoloader
troller::class, 'create']);
$router->addRoute('PUT', '/users/{id/d}/{text/t}', [MyCustomController::class, 'update']);
$router->addRoute('PATCH', '/users/{id/d}', [MyCustomController::class, 'modify']);
$router->addRoute('DELETE', '/users/{id/d}/{text/t}', [MyCustomController::class, 'delete']);
try {
// Get request method and URI
$method = $_SERVER['REQUEST_METHOD'];
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Match the route
[$routeHandler, $params] = $router->match($method, $uri);
if ($routeHandler !== null) {
// Execute the corresponding method based on the matched route
[$controller, $method] = $routeHandler;
$controllerInstance = new $controller();
// Handle the matched route with extracted parameters dynamically as an array
$response = call_user_func_array([$controllerInstance, $method], [$params]);
// Output the response or handle it further
echo $response;
} else {
throw new RouteNotFoundException('Route not found.');
}
} catch (RouteNotFoundException $e) {
// Handle RouteNotFoundException
http_response_code(404);
echo '404 Not Found: ' . $e->getMessage();
} catch (MethodNotAllowedException $e) {
// Handle MethodNotAllowedException
http_response_code(405);
echo '405 Method Not Allowed: ' . $e->getMessage();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.