Skip to content

Commit bedd63c

Browse files
author
Петр Кленкин
committed
fix docs
1 parent a7c31f4 commit bedd63c

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

docs/bxrouter.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Инструкция по настройке роутера Bitrix
2+
3+
1. Внутри файла /bitrix/.settings.php добавьте секцию
4+
```php
5+
'routing' =>
6+
array (
7+
'value' =>
8+
array (
9+
'config' =>
10+
array (
11+
'custom_routes.php'
12+
),
13+
),
14+
'readonly' => true,
15+
),
16+
```
17+
2. Внутри папки bitrix/routes добавьте файл custom_routes.php с содержимым:
18+
```php
19+
<?php
20+
21+
use Bitrix\Main\Routing\RoutingConfigurator;
22+
23+
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/local/routes/custom_routes.php')) {
24+
$customRoutesClosure = include $_SERVER['DOCUMENT_ROOT'] . '/local/routes/custom_routes.php';
25+
26+
if ($customRoutesClosure instanceof Closure) {
27+
return $customRoutesClosure;
28+
}
29+
}
30+
31+
return function (RoutingConfigurator $routingConfigurator) {
32+
//
33+
};
34+
```
35+
3. Внутри папки local/routes создайте файл custom_routes.php с содержимым:
36+
```php
37+
<?php
38+
39+
use Bitrix\Main\ModuleManager;
40+
use Bitrix\Main\Routing\RoutingConfigurator;
41+
42+
require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
43+
44+
/**
45+
* Получение машрутов установленных в local модулей
46+
*
47+
* Сделанно анонимной функцией, чтобы все переменные
48+
* имели локальный пространство имен
49+
*
50+
* Поскольку переменные с этими иминами используется ранее в ядре
51+
*
52+
* @return array
53+
*/
54+
$getRoutePaths = static function (): array {
55+
foreach (ModuleManager::getInstalledModules() as $module) {
56+
$route = $_SERVER['DOCUMENT_ROOT'] . '/local/modules/' . $module['ID'] . '/routes.php';
57+
if (file_exists($route)) {
58+
$routes[] = $route;
59+
}
60+
}
61+
return $routes ?? [];
62+
};
63+
64+
return function (RoutingConfigurator $routingConfigurator) use ($getRoutePaths) {
65+
foreach ($getRoutePaths() as $route) {
66+
$callback = include $route;
67+
if ($callback instanceof Closure) {
68+
$callback($routingConfigurator);
69+
}
70+
}
71+
};
72+
73+
```
74+
75+
4. Теперь в каждом модуле вы можете создавать файл routes.php, где регистрировать свои маршруты.
76+

0 commit comments

Comments
 (0)