Views: 4502
Last Modified: 25.01.2022
Queries
Route description starts from defining a query method. There are 3 combinations of methods available:
$routes->get('/countries', function () {
// triggered only by GET query
});
$routes->post('/countries', function () {
// triggered only by POST query
});
$routes->any('/countries', function () {
// triggered only by any query type
});
Use the method methods for specifying arbitrary set of methods:
$routes->any('/countries', function () {
// triggers any type of query type
})->methods(['GET', 'POST', 'OPTIONS']);
Parameters
Use curly brackets to define parameter in the address:
$routes->get('/countries/{country}', function ($country) {
return "country {$country} response";
});
By default, [^/]+
pattern is used for parameters. The where
route method is used for indicating your own criteria:
$routes->get('/countries/{country}', function ($country) {
return "country {$country} response";
})->where('country', '[a-zA-Z]+');
If parameter value can contain /, use the pattern .*
:
$routes->get('/search/{search}', function ($search) {
return "search {$search} response";
})->where('search', '.*');
Parameters can have default values; in this case their presence is optional in the address:
$routes->get('/countries/{country}', function ($country) {
return "country {$country} response";
})->default('country', 'Australia');
// route will be selected when querying /countries/
// the country parameter will have a specified value
For convenience, you can set parameters that don't participate in generating the address at all:
$this->routes->get('/countries/hidden', function ($viewMode) {
return 'countries response {$viewMode}';
})->default('viewMode', 'custom');
Access to route parameter values is gained via controller parameters or current route object:
$routes->get('/countries/{country}', function ($country) {
return "country {$country} response";
});
...
$app = \Bitrix\Main\Application::getInstance();
$country = $app->getCurrentRoute()->getParameterValue('country');
Names
Assign this route a unique identifier - a name - for convenience and route lists systematization:
$routes->get('/path/with/name', function () {
return 'path with name';
})->name('some_name');
Subsequently, this will allow to query the route when generating links.
Controllers
Routing supports several types of controllers:
- Controllers Bitrix\Main\Engine\Controller:
$routes->get('/countries', [SomeController::class, 'view']);
// launches action SomeController::viewAction()
- Separate controller actions Bitrix\Main\Engine\Contract\RoutableAction:
$routes->get('/countries', SomeAction::class);
- Closures:
$routes->get('/countries/', function () {
return "countries response";
});
Arguments can be a query object Bitrix\Main\HttpRequest, current route object Bitrix\Main\Routing\Route, as well as route named parameters in any combinations:
use Bitrix\Main\HttpRequest;
use Bitrix\Main\Routing\Route;
$routes->get('/countries/{country}', function ($country, HttpRequest $request) {
return "country {$country} response";
});
$routes->get('/countries/{country}', function (Route $route) {
return "country {$route->getParameterValue('country')} response";
});
- The class Bitrix\Main\Routing\Controllers\PublicPageController is available for purposes of backward compatibility with public pages:
$routes->get('/countries/', new PublicPageController('/countries.php'));