RouterRegister Help

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.

#[Group('post', GroupEnum::frontend)] #[Prefix('post')] enum PostRoute: string implements RouteInterface { use RouteTrait; case index = ''; case create = '/create'; case update = '/update/{id}'; case view = '/{id}'; }

The generated routes will be something like (class names will be FQCNs):

Group::create('/post') ->namePrefix('post.') ->routes( Route::methods(['GET'], '') ->name('index') ->action([PostController::class, 'index']) , Route::methods(['GET', 'POST'], '/create') ->name('create') ->action([PostController::class, 'create']) , Route::methods(['GET', 'POST'], '/update/{id: [1-9]\d*}') ->name('update') ->action([PostController::class, 'update']) , Route::methods(['GET'], '/view/{id: [1-9]\d*}') ->name('view') ->action([PostController::class, 'view']) , )

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.

#[Group('post', GroupEnum::frontend)] #[Prefix('post')] enum PostRoute: string implements RouteInterface { use RouteTrait; case index = '//posts'; case create = '/create'; case update = '/update/{id}'; case view = '/{id}'; }

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()

Last modified: 06 December 2025