Views: 3276
Last Modified: 25.01.2022
Merging in groups
In case of similar attributes for several routes, it's recommended to merge them into groups:
$routes->group(function (RoutingConfigurator $routes) {
$routes->get('/path1, function () {});
$routes->get('/path2, function () {});
$routes->get('/path3, function () {});
});
Merging itself doesn't affect the system behaviour and is useful in case of shared attributes: parameters, prefix or name, that will be overviewed below.
Group parameters
When several routes have a shared parameter, it makes sense to move it up to a group level. This will allow avoid describing this parameter separately for each route:
$routes
->where('serviceCode', '[a-z0-9]+')
->group(function (RoutingConfigurator $routes) {
$routes->get('/{serviceCode}/info', [ServicesController::class, 'info']);
$routes->get('/{serviceCode}/stats', [ServicesController::class, 'stats']);
});
Group prefix
If the address start matches for several routes, move it as general for the group:
$routes->prefix('about')->group(function (RoutingConfigurator $routes) {
$routes->get('company', function () {});
$routes->get('personal', function () {});
$routes->get('contact', function () {});
});
The example above shows route addresses as /about/company, /about/personal and /about/contact, to avoid duplicating the general portion.
Group name
General portion of route name is generated in the similar manner as the prefix:
$routes
->prefix('about')
->name('about_')
->group(function (RoutingConfigurator $routes) {
$routes->name('company')->get('company', function () {});
$routes->name('personal')->get('personal', function () {});
$routes->name('contact')->get('contact', function () {});
})
;
The example above shows route names as about_company, about_personal and about_contact.