-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Петр Кленкин
committed
May 18, 2021
1 parent
a7c31f4
commit bedd63c
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Инструкция по настройке роутера Bitrix | ||
|
||
1. Внутри файла /bitrix/.settings.php добавьте секцию | ||
```php | ||
'routing' => | ||
array ( | ||
'value' => | ||
array ( | ||
'config' => | ||
array ( | ||
'custom_routes.php' | ||
), | ||
), | ||
'readonly' => true, | ||
), | ||
``` | ||
2. Внутри папки bitrix/routes добавьте файл custom_routes.php с содержимым: | ||
```php | ||
<?php | ||
|
||
use Bitrix\Main\Routing\RoutingConfigurator; | ||
|
||
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/local/routes/custom_routes.php')) { | ||
$customRoutesClosure = include $_SERVER['DOCUMENT_ROOT'] . '/local/routes/custom_routes.php'; | ||
|
||
if ($customRoutesClosure instanceof Closure) { | ||
return $customRoutesClosure; | ||
} | ||
} | ||
|
||
return function (RoutingConfigurator $routingConfigurator) { | ||
// | ||
}; | ||
``` | ||
3. Внутри папки local/routes создайте файл custom_routes.php с содержимым: | ||
```php | ||
<?php | ||
|
||
use Bitrix\Main\ModuleManager; | ||
use Bitrix\Main\Routing\RoutingConfigurator; | ||
|
||
require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php'; | ||
|
||
/** | ||
* Получение машрутов установленных в local модулей | ||
* | ||
* Сделанно анонимной функцией, чтобы все переменные | ||
* имели локальный пространство имен | ||
* | ||
* Поскольку переменные с этими иминами используется ранее в ядре | ||
* | ||
* @return array | ||
*/ | ||
$getRoutePaths = static function (): array { | ||
foreach (ModuleManager::getInstalledModules() as $module) { | ||
$route = $_SERVER['DOCUMENT_ROOT'] . '/local/modules/' . $module['ID'] . '/routes.php'; | ||
if (file_exists($route)) { | ||
$routes[] = $route; | ||
} | ||
} | ||
return $routes ?? []; | ||
}; | ||
|
||
return function (RoutingConfigurator $routingConfigurator) use ($getRoutePaths) { | ||
foreach ($getRoutePaths() as $route) { | ||
$callback = include $route; | ||
if ($callback instanceof Closure) { | ||
$callback($routingConfigurator); | ||
} | ||
} | ||
}; | ||
|
||
``` | ||
|
||
4. Теперь в каждом модуле вы можете создавать файл routes.php, где регистрировать свои маршруты. | ||
|