nette/routing
Fast, flexible URL routing for PHP apps. Define clean routes with parameters, optional parts, and strict matching. Supports route lists, host/path based rules, URL generation, and reverse routing—ideal for structured web apps and APIs.
Install the package via Composer: composer require nette/routing. Begin by instantiating a RouteList, typically in a dedicated routes.php file or service container definition. Define simple routes using addRoute() with pattern strings (e.g., 'posts/<id>') and controller/action references (e.g., 'Post:show') or closures. For request handling, inject the RouteList into your bootstrap and call $router->match($httpRequest)—this returns matched route parameters as an array (e.g., ['action' => 'show', 'id' => '123']) or null if no match. Start with basic static and parameterized routes before advancing to constraints and scoping.
RouteList::withPath() to scope routes under prefixes (e.g., /api, /admin) or RouteList::withDomain() for subdomains. Group related routes by feature using separate RouteList instances composed via addList().'<id:\d+>', '<slug:[\p{L}\p{N}_-]+>')—especially critical for public-facing URLs to prevent over-matching. Combine with defaults (e.g., '<action=default>') for optional segments.constructUrl() with a parameter array to generate URLs and reuse those same patterns/defaults in addRoute(). This avoids discrepancies between outgoing links and inbound handling.$router->match($netteRequest), then constructs a PSR-7 ServerRequestInterface from matched parameters—ideal for migrating legacy Nette code or hybrid architectures.->setFlags(['admin' => true])) and inspect them via getFlags() or getRoute()—useful for ACL checks or feature toggling in route processing.posts/<id> before posts/new). Use getRoutes() to inspect runtime ordering.withPath() treats trailing slashes as optional, manually define routes consistently (e.g., always posts/ or never posts) to prevent subtle redirect loops or 404s.nette/routing:^2.5 instead—check composer.json for exact version compatibility.param2path() correctly handles RFC-compliant reserved characters (e.g., @, !, ~). Never pre-encode values passed to constructUrl()—it will double-encode.Closure, objects) as default parameter values unless using ≥v3.0.3—older versions throw exceptions when serializing defaults during URL building.Route::match($httpRequest) individually on specific routes (not the list) to isolate failing patterns. Use Route::getFlags() and Route::getPattern() to log effective route configurations.How can I help you explore Laravel packages today?