PHP code example of stougeiro / router

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

    

stougeiro / router example snippets


use STDW\Cache\Cache;
use STDW\Cache\CacheConfig;
use STDW\Http\Router\RouteCollection;
use STDW\Http\Router\Router;

$cache = new Cache(new CacheConfig([
    'handler' => 'sqlite',
    'storage' => __DIR__ . '/cache',
    'associative' => true,
]));

$collection = new RouteCollection($cache);
$collection->load(__DIR__ . '/routes.php');

$router = new Router($collection, $cache, true);
$route = $router->match($request);

// $route->getController() → controller class
// $route->getVariables() → ['id' => '123']



return [
    '/users' => App\Controllers\UserController::class,
    '/users/{id:num}' => [App\Controllers\UserController::class => 'users.show'],
    '/posts/{slug:slug}' => [App\Controllers\PostController::class => 'posts.show'],
];