Routes
Route Enumerations
Routes are defined in string backed Enumerations that implement RouteInterface (part of the RouterRegister package). Each Controller should have an associated Route Enumeration.
Each case in a Route Enumeration defines a route.
The case name defines the route name.
The case value defines the route pattern, including route parameter names.
Route Pattern Definition
Route parameters are defined by the parameter name enclosed in braces, with optional parameters additionally enclosed in square brackets.
The generated routes will be something like (class names will be FQCNs):
The resulting route names and URLs are:
Case | Name | URL |
|---|---|---|
index | {group name prefix}post.index | {group route prefix}/post |
create | {group name prefix}post.create | {group route prefix}/post/create |
update | {group name prefix}post.update | {group route prefix}/post/update/<id> |
view | {group name prefix}post.view | {group route prefix}/post/<id> |
Route Hoisting
If a route should not have the route prefix applied, it can be hoisted out of the controller group by starting the pattern with //. For example, if the index Case should match /posts, the route pattern should be //posts.
A consequence of hoisting a route out of the class Group is that Route Enumeration Attributes are not applied to it. If any of the Route Enumeration Attributes should be applied to a hoisted route, specify the required attributes at Method Level.
The resulting URLs and route names are:
Case | Name | URL |
|---|---|---|
index | {group name prefix}post.index | {group route prefix}/posts |
create | {group name prefix}post.create | {group route prefix}/post/create |
update | {group name prefix}post.update | {group route prefix}/post/update/{id} |
view | {group name prefix}post.view | {group route prefix}/post/view/{id} |
Fallback Route
RouterRegister allows one route in an application level group to be designated as the Fallback route; this is route is placed last in the application level group's route collection making it the last route that a router will attempt to match.
A Fallback route is hoisted out of its Controller level group. As with other hoisted routes, a Controller level Route Prefix does not apply but the Controller level Name Prefix does.
RouteInterface and RouteTrait
RouteInterface
Route Interface defines the following method:
- getRouteName()
Returns the fully qualified route name; useful when generating URLs
RouteTrait
RouteTrait implements RouteInterface and can be used in Route Enumerations.
RouteTrait also supports obtaining the fully qualified route name by calling the Route Enumeration Case.
Example: PostRoute::index() is the equivalent of PostRoute::index->getRouteName()