PHP code example of echo-fusion / routemanager

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

    

echo-fusion / routemanager example snippets


use EchoFusion\RouteManager\Router;
use EchoFusion\RouteManager\RouteMatcher\RouteMatcher;

$routeMatcher = new RouteMatcher();
$router = new Router($routeMatcher);

use Psr\Http\Message\ServerRequestInterface;

// simple route
$router->post(
    name: 'api.store', 
    path: '/api', 
    action: [ApiController::class, 'store']
);

// route parameter with regex constraints
$router->get(
    name: 'blog-show',
    path: '/post/{id}/detail/{slug}',
    action: function(int $id, string $slug,  ServerRequestInterface $request) {
        // write your code here...           
        var_dump($id , $slug);
    }),
    constraints: [
        'id' => '[0-9]+',
        'slug' => '[a-z0-9\-]+',
    ]
);

try {
    $routeMatch = $router->dispatch($request);
} catch (RouteNotFoundException $e) {
    // Handle route not found
} catch (Throwable $exception) {
    // Handle any other errors
}

$action = $routeMatch->getRoute()->getAction();

// Get the route parameters and request
$routeParams = $routeMatch->getParams();
$params = array_merge($routeParams, [$request]);

// Call the action (callable or controller method) with type hinting
return call_user_func_array($action, $params);
 bash
$ composer