PHP code example of wilycode / router

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

    

wilycode / router example snippets




declare(strict_types=1);

use Psr\Http\Message\ResponseInterface;
use WilyCode\Router\RouteCollection;
use WilyCode\Router\Router;
use WilyCode\Router\Rule\IntRule;

$routes = new RouteCollection();

$routes->get(
    name: 'home',
    path: '/',
    handler: static fn (): ResponseInterface => $response,
);

$routes->get(
    name: 'articles.show',
    path: '/articles/{id}',
    handler: static function (int $id) use ($response): ResponseInterface {
        // IntRule validates and casts {id} to an integer.
        return $response;
    },
    rules: ['id' => IntRule::class],
);

$router = new Router($container);
$router->addCollection($routes);

$result = $router->dispatch($request); // ResponseInterface
$path = $router->generatePath('articles.show', ['id' => 42]); // /articles/42

$routes->get('users.index', '/users', [UserController::class, 'index']);
$routes->post('users.store', '/users', [UserController::class, 'store']);
$routes->put('users.update', '/users/{id}', [UserController::class, 'update']);
$routes->delete('users.delete', '/users/{id}', [UserController::class, 'delete']);

$routes->get(
    name: 'users.show',
    path: '/users/{id}',
    handler: [UserController::class, 'show'],
);

$router->generatePath('users.show', ['id' => 15]); // /users/15

use WilyCode\Router\Rule\IntRule;
use WilyCode\Router\Rule\BoolRule;

$routes->get(
    name: 'articles.show',
    path: '/articles/{id}',
    handler: [ArticleController::class, 'show'],
    rules: ['id' => IntRule::class],
);

$routes->get(
    name: 'features.show',
    path: '/features/{enabled}',
    handler: [FeatureController::class, 'show'],
    rules: ['enabled' => BoolRule::class],
);

use WilyCode\Router\Rule\RegexRule;

$routes->get(
    name: 'codes.show',
    path: '/codes/{code}',
    handler: [CodeController::class, 'show'],
    rules: ['code' => new RegexRule('[A-Z]{2}[0-9]{3}')],
);

use WilyCode\Router\Rule\UuidRule;
use WilyCode\Router\Rule\SlugRule;
use WilyCode\Router\Rule\EnumRule;
use WilyCode\Router\Rule\DateRule;

$routes->get('api.resource', '/api/resources/{id}', [ApiController::class, 'show'],
    rules: ['id' => UuidRule::class]);

$routes->get('blog.article', '/blog/{slug}', [BlogController::class, 'show'],
    rules: ['slug' => SlugRule::class]);

$routes->get('posts.byStatus', '/posts/status/{status}', [PostController::class, 'list'],
    rules: ['status' => new EnumRule(['draft', 'published', 'archived'])]);

$routes->get('calendar.day', '/calendar/{date}', [CalendarController::class, 'show'],
    rules: ['date' => DateRule::class]);

$router->generatePath('blog.article', ['slug' => 'hello-world']);
// /blog/hello-world

$router->generatePath('posts.byStatus', ['status' => 'published']);
// /posts/status/published

$router->generatePath('calendar.day', ['date' => new DateTimeImmutable('2026-10-13')]);
// /calendar/2026-10-13

$router->generatePath('blog.article', ['slug' => 'Bad Slug']);
// Throws InvalidArgumentException.

$routes->get(
    name: 'articles.index',
    path: '/articles[/{page}]',
    handler: [ArticleController::class, 'index'],
    defaults: ['page' => 1],
    rules: ['page' => IntRule::class],
);

$admin = (new RouteCollection())
    ->prefix('/admin')
    ->middleware(AuthenticateMiddleware::class);

$admin->get(
    name: 'articles.edit',
    path: '/articles/{id}/edit',
    handler: [ArticleController::class, 'edit'],
    rules: ['id' => IntRule::class],
);

$router->addCollection($admin);

$api = (new RouteCollection())
    ->prefix('/api')
    ->middleware(AuthenticationMiddleware::class);

$api->get(
    name: 'api.profile',
    path: '/profile',
    handler: [ProfileController::class, 'show'],
    middlewares: [AuditMiddleware::class],
);

$routes->get(
    name: 'articles.show',
    path: '/articles/{id}',
    handler: [ArticleController::class, 'show'],
    rules: ['id' => IntRule::class],
);

$path = $router->generatePath('articles.edit', ['id' => 42]);
// /admin/articles/42/edit

use WilyCode\Router\UrlGenerator;

$urls = new UrlGenerator($router);

$url = $urls->generate(
    'articles.edit',
    ['id' => 42],
    ['tab' => 'seo', 'lang' => 'en'],
    'details',
);
// /admin/articles/42/edit?tab=seo&lang=en#details

$routes->get(
    name: 'articles.special',
    path: '/articles/{slug}',
    handler: [ArticleController::class, 'special'],
    priority: 10,
);

use WilyCode\Router\CompiledRoutes;
use WilyCode\Router\CompiledRoutesBuilder;

$manifest = (new CompiledRoutesBuilder())->build([$routes]);

$compiledRouter = new Router($container);
$compiledRouter->setCompiledRoutes(new CompiledRoutes($manifest));

use WilyCode\Router\Observability\RoutingObserverInterface;

$observer = new class implements RoutingObserverInterface {
    public function around(string $phase, callable $callback): mixed
    {
        // Measure or record the phase if needed.
        return $callback();
    }
};

$router = new Router($container, $observer);

use WilyCode\Router\NotFoundException;

try {
    $response = $router->dispatch($request);
} catch (NotFoundException $exception) {
    // Create and return your application's PSR-7 404 response.
}