Skip to content

Commit 4dfd4e4

Browse files
committed
Правка readme.MD
1 parent 8ecec90 commit 4dfd4e4

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

readme.MD

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,99 @@ $this->config = Configuration::getInstance()->get('my_config') ?? [];
280280
// Из модуля
281281
$this->parameters = Configuration::getInstance('my.module')->get('parameters') ?? [];
282282
$this->services = Configuration::getInstance('my.module')->get('services') ?? [];
283-
```
283+
```
284+
285+
### Совместимость с новым механизмом битриксовых роутов
286+
287+
С версии `21.400.0` (от 16.07.2021) главного модуля в Битриксе появился [сносный](https://dev.1c-bitrix.ru/learning/course/index.php?COURSE_ID=43&CHAPTER_ID=013764&LESSON_PATH=3913.3516.5062.13764) роутер.
288+
289+
Чтобы использовать в этом контексте контейнер нужно:
290+
291+
- В файле описания маршрутов (например, `/local/routes/web.php`) в самом верху подключить ядро.
292+
293+
Это важно, т.к. без этого сервис-провайдер завалится на стадии подключения файла с роутами; они подключаются раньше инициализации ядра.
294+
И, если эту проблему еще можно решить, отключив проверку классов сервисов на существование, то запускающиеся автоматом сервисы по тэгу
295+
`service.bootstrap` обломятся стопроцентно.
296+
297+
```php
298+
use Local\ExampleBitrixActionController;
299+
use Prokl\ServiceProvider\ServiceProvider;
300+
use Bitrix\Main\Routing\Controllers\PublicPageController;
301+
use Bitrix\Main\Routing\RoutingConfigurator;
302+
303+
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
304+
305+
$container = ServiceProvider::instance();
306+
307+
return function (RoutingConfigurator $routes) use ($container) {
308+
309+
$routes->get('/countries3/{country}/', [$container->get(ExampleBitrixActionController::class), 'cacheAction'])
310+
->default('country', 'Russia')
311+
->name('first_bitrix_route')
312+
;
313+
314+
$routes->get('/', new PublicPageController('/index.php')); // Старый роут на статике.
315+
316+
};
317+
```
318+
319+
Класс битриксового контроллера (`ExampleBitrixActionController`) с заточкой под DI:
320+
321+
```php
322+
namespace Local;
323+
324+
use Bitrix\Main\Engine\Contract\RoutableAction;
325+
use Bitrix\Main\Engine\Controller;
326+
use Symfony\Component\HttpKernel\KernelInterface;
327+
328+
class ExampleBitrixActionController extends Controller implements RoutableAction
329+
{
330+
/**
331+
* @var KernelInterface $kernel
332+
*/
333+
private $kernel;
334+
335+
public function __construct(KernelInterface $kernel)
336+
{
337+
$this->kernel = $kernel;
338+
parent::__construct();
339+
}
340+
341+
/**
342+
* @return string|Controller
343+
*/
344+
public static function getControllerClass() {
345+
return ExampleBitrixActionController::class;
346+
}
347+
348+
/**
349+
* @return string
350+
*/
351+
public static function getDefaultName() {
352+
return 'testingAction';
353+
}
354+
355+
public function cacheAction(string $country)
356+
{
357+
return ['cacheDir' => $this->kernel->getCacheDir(), 'country' => $country];
358+
}
359+
360+
public function configureActions()
361+
{
362+
return [
363+
'cache' => [
364+
'prefilters' => [], 'postfilters' => [],
365+
],
366+
];
367+
}
368+
}
369+
```
370+
Описывается сервисом так:
371+
372+
```yaml
373+
Local\ExampleBitrixActionController:
374+
arguments: ['@kernel']
375+
```
376+
377+
Ничего революционного, но так можно получить нормально-сконфигурированный класс контроллера,
378+
со всякими зависимостями и т.п.

0 commit comments

Comments
 (0)