PHP code example of alexpts / php-routing

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

    

alexpts / php-routing example snippets


use PTS\Routing\Route;
use PTS\Routing\CollectionRoute;
use PTS\Routing\Matcher;
use PTS\Routing\RouteService;
use Psr\Http\Message\RequestInterface;

$route = new Route('/', function() {
    return ['response' => 'data'];
});
$collection = new CollectionRoute();
$collection->add('main', $route);
$matcher = new Matcher(new RouteService());

$activeRoute = $matcher->match($collection, '/')->current();
$response = $activeRoute($request); // PSR-7 request

use PTS\Routing\Route;
use PTS\Routing\CollectionRoute;
use PTS\Routing\Matcher;
use PTS\Routing\RouteService;
use Psr\Http\Message\RequestInterface;

$route = new Route('/users/{userId}/', function($userId) {
    return $userId;
});
$route->pushMiddleware(new CallWithMatchParams);

$collection = new CollectionRoute();
$collection->add('user', $route);
$matcher = new Matcher(new RouteService());

$activeRoute = $matcher->match($collection, '/users/4/')->current();