Skip to content

Commit

Permalink
Конфигурирование нативных роутов Битрикса через Yaml файл
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 24, 2021
1 parent 1de2766 commit 7ce8df4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
12 changes: 10 additions & 2 deletions Services/Utils/BitrixRouteConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Prokl\BitrixSymfonyRouterBundle\Services\Utils;

use Bitrix\Main\Engine\Controller;
use Bitrix\Main\Routing\Controllers\PublicPageController;
use Bitrix\Main\Routing\RoutingConfigurator;
use LogicException;
use RuntimeException;
Expand Down Expand Up @@ -75,6 +76,13 @@ public function convertRoute(string $name, Route $route, RoutingConfigurator $ro
}

$path = $route->getPath();

// Старые статические страницы
if ($route->getDefault('_public') === true) {
$routes->get($path, new PublicPageController($path . 'index.php'));
return;
}

$controller = $this->parseControllerString($name, $route->getDefault('_controller'));

if (!is_subclass_of($controller[0], Controller::class)) {
Expand Down Expand Up @@ -104,7 +112,7 @@ public function convertRoute(string $name, Route $route, RoutingConfigurator $ro
$processedRoute = $routes->any($path, [$service, $controller[1]]);

$processedRoute = $processedRoute->methods($methods)
->name($name);
->name($name);

foreach ($route->getRequirements() as $reqParam => $reqValue) {
$processedRoute = $processedRoute->where($reqParam, $reqValue);
Expand All @@ -130,7 +138,7 @@ public function convertRoute(string $name, Route $route, RoutingConfigurator $ro
private function parseControllerString(string $name, $controller) : array
{
$argument = $controller;
if (is_string($controller)) {
if (is_string($controller) && $controller) {
if (strpos($controller, '::') !== false) {
$controller = explode('::', $controller, 2);
if (strpos($controller[1], 'Action') === false) {
Expand Down
8 changes: 7 additions & 1 deletion Tests/Fixture/bitrix_routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ first_bitrix_route:
defaults:
param: 'Russia'


# Старые статические страницы
# / => /index.php
public_page:
path: /
controller: ''
defaults:
_public: true # Ключевой признак
20 changes: 13 additions & 7 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ $routeConvertor->setContainer($container);
return function (RoutingConfigurator $routes) use ($container, $routeConvertor, $routeCollection) {

$routeConvertor->convertRoutes($routes);

$routes->get('/', new PublicPageController('/index.php'));

};
```

Expand Down Expand Up @@ -96,13 +93,22 @@ return function (RoutingConfigurator $routes) use ($container, $routeConvertor,

```yaml
first_bitrix_route:
path: /countries/{country}/
controller: 'Local\ExampleBitrixActionController::cacheAction'
path: /foo/{param}/
controller: 'Prokl\BitrixSymfonyRouterBundle\Tests\Fixture::cacheAction'
methods: GET|POST
requirements:
country: '\d+'
param: '\d+'
defaults:
country: 'Russia'
param: 'Russia'

# Старые статические страницы
# / => /index.php
public_page:
path: /
controller: ''
defaults:
_public: true # Ключевой признак

```

Если установлен Битрикс с версией модуля младше `21.400.0`, то соответствующие сервисы будут
Expand Down

0 comments on commit 7ce8df4

Please sign in to comment.