Views: 3947
Last Modified: 25.01.2022
Routes with name
When describing a route, set a unique name for it:
$routes->get('/countries/{country}', function () {
return 'some output';
})->name('country_detail');
And use this name for generating a link:
$router = \Bitrix\Main\Application::getInstance()->getRouter();
$url = $router->route('country_detail', ['country' => 'Australia']);
// $url: /countries/Australia
Names act as unique identifiers. When link format must be changed, i. e. its static portion:
- $routes->get('/countries/{country}', function () {
+ $routes->get('/countries/{country}', function () {
return 'some output';
})->name('country_detail');
In this case, you don't have to change all links to this route, because they use specifically name for forwarding.
Routes without name
When unique name is not specified for the route, its permissible to indicate its address in the link. Helper \Bitrix\Main\Routing\Router::url() may be useful for an available GET parameters:
$country = 'Australia';
$router = \Bitrix\Main\Application::getInstance()->getRouter();
$url = $router->url("/contries/{$country}", [
'showMoreDetails' => 1
]);
// $url: /contries/Australia?showMoreDetails=1