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
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.
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.