Named Routes
Register named routes with the name argument or as() method and generate URLs to them later.
You can register routes in ColdBox with a human friendly name so you can reference them later for link generation and more.
Registering Named Routes
You will do this in two forms:
Using the
route()method and thenameargumentUsing the
as()method
// Using the pattern by convention
route( pattern="/healthcheck" ).to( "healthcheck" );
// Using the name argument
route(
pattern = "/users/list",
target = "users.index",
name = "usermanager"
);
route(
pattern = "/user/:id/profile",
target = "users.show",
name = "userprofile"
);
// Using the as() method
route( "/users/:id/profile" )
.as( "usersprofile" )
.to( "users.show" )Generating URLs to Named Routes
You will generate URLs to named routes by leveraging the route() method in the request context object (event).
Let's say you register the following named routes:
Then we can create routing URLs to them easily with the event.route() method:
Inspecting The Current Route
The request context object (event) also has some handy methods to tell you the name or even the current route that was selected for execution:
getCurrentRouteName()- Gives you the name of the current route, if anygetCurrentRoute()- Gives you the currently executed routegetCurrentRouteRecord()- Gives you the currently executed route's full record/structgetCurrentRouteMeta()- Gives you the metadata struct assigned to the current route via themeta()modifiergetCurrentRoutedURL()- Gives you the complete routed URL pattern that matched the routegetCurrentRoutedNamespace()- Gives you the current routed namespace, if anyrouteIs( name )- Boolean check to verify if the passed name is the same as the current route's nameurlMatches( path, exact=false )- Boolean check to test if the givenpathexists (or, whenexactistrue, matches exactly) within the currently routed URLurlMatchesExact( path )- Boolean check, shortcut tourlMatches( path, true ), to test if the givenpathmatches the currently routed URL exactly
Last updated
Was this helpful?