joomla/router
Joomla Framework Router registers application routes and dispatches incoming request URIs to controller methods. PHP 8.1+ compatible, install via Composer (joomla/router ~3.0). Suitable for building clean, maintainable routing in PHP apps.
Install the package via Composer: composer require joomla/router "~3.0" (v3.x requires PHP 8.1+, v4.x requires PHP 8.3+). Begin by instantiating Joomla\Router\Router, then register routes using addRoute() with HTTP method, pattern (including named placeholders like :id), and a callable target (controller class/method, closure, or callable string). Use parseRoute($uri) to match a path and extract matched variables. The simplest first use case is mapping REST-style endpoints to controller methods, e.g.:
$router = new Router();
$router->addRoute('GET', '/api/users/:id', 'App\Controller\UserController@show', ['id' => '\d+']);
$match = $router->parseRoute('/api/users/123');
// $match->getController() and $match->getVariables() provide structured access (v2+)
Start with the README and docs/overview.md—they contain core examples and clarify that the package only handles routing logic; controller execution remains your responsibility.
addRoute() to enforce type-specific variables (e.g., ['id' => '\d+'] for IDs), ensuring cleaner validation upstream.addRoute() to inject non-path-based data (e.g., 'requires_auth' => true, 'version' => 'v2') into the resolved route via the defaults array—accessible through ResolvedRoute::getDefaults() in v2+.joomla/console, add DebugRouterCommand to list all configured routes, helpful for introspecting API endpoints during development.parseRoute() with dynamic controller dispatch, e.g., $controller = new $route->getController(); $controller->handle($route->getVariables());, or use call_user_func_array() for lightweight setups.GET, POST, etc.) when adding routes to avoid accidental cross-method dispatch. Match patterns like /resource/:id only for reads, and /resource for collections.parseRoute() now returns a ResolvedRoute object, not an array—access via getController(), getVariables(), and getDefaults() instead of array indexing.([^/]*); be explicit about regex when matching non-string types (e.g., integers) to avoid malformed matches.strtolower()) before parsing if case-insensitivity is required.3.x-dev and newer) requires PHP 8.3.How can I help you explore Laravel packages today?