What is CodeIgniter 4 named routes and URL generation?

Answer

CodeIgniter 4 supports named routes for generating URLs by name instead of hardcoding paths. Assign names in app/Config/Routes.php: $routes->get("users/(:num)", "UserController::show/$1", ["as" => "user.show"]). Generate URLs by name: route_to("user.show", 42) returns /users/42. In views: <a href="<?= route_to("user.show", $user->id) ?>">View</a>. Reverse routing also works with controller::method notation: route_to("UserController::show", $id). Redirect to named route: return redirect()->route("user.show", [$id]). Named routes decouple views and controllers from URL structures — change the URL in one place and all generated links automatically update. This is especially valuable in large applications and when implementing URL slugs or localized URLs that change per-language.