Named Routes
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:
  1. 1.
    Using the route() method and the name argument
  2. 2.
    Using the as() method
1
// Using the name argument
2
route(
3
pattern = "/users/list",
4
target = "users.index",
5
name = "usermanager"
6
);
7
8
route(
9
pattern = "/user/:id/profile",
10
target = "users.show",
11
name = "userprofile"
12
);
13
14
// Using the as() method
15
route( "/users/:id/profile" )
16
.as( "usersprofile" )
17
.to( "users.show" )
Copied!

Generating URLs to Named Routes

You will generate URLs to named routes by leveraging the route() method in the request context object (event).
1
route(
2
// The name of the route
3
required name,
4
// The params to pass, can be a struct or array
5
struct params={},
6
// Force or un-force SSL, by default we keep the same protocol of the request
7
boolean ssl
8
);
Copied!
Let's say you register the following named routes:
1
route(
2
pattern = "/users/list",
3
target = "users.index",
4
name = "usermanager"
5
);
6
7
route(
8
pattern = "/user/:id/profile",
9
target = "users.show",
10
name = "userprofile"
11
);
Copied!
Then we can create routing URLs to them easily with the event.route() method:
1
<!-- Named Route with no params -->
2
<a href="#event.route( 'usermanager' )#">Manage Users</a>
3
4
<!-- Named Route with struct params -->
5
<a href="#event.route( 'userprofile', { id = 3 } )#">View User</a>
6
7
<!-- Named Route with array params -->
8
<a href="#event.route( 'userprofile', [ 3 ] )#">View User</a>
Copied!

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 any
  • getCurrentRoute() - Gives you the currently executed route
  • getCurrentRoutedURL() - Gives you the complete routed URL pattern that matched the route
  • getCurrentRoutedNamespace() - Gives you the current routed namespace, if any
Last modified 3yr ago