A
A
Alexander Pankov2020-11-30 13:34:47
PHP
Alexander Pankov, 2020-11-30 13:34:47

What is the best practice for connecting DI in Bitrix?

hello, can I read somewhere or ask you about connecting DI in Bitrix?

I want to put the simplest pimple container, https://pimple.symfony.com/

in order to pull the necessary services and classes from it, I'm interested in the specifics:
1) where is it better to write (dbconn\init) its initialization,
2) how to declare the container itself, so that it is convenient reach it (global)?

now I am writing composer autoload in dbconn and loading environment variables

if (!file_exists($autoloadPathDbconn= realpath(__DIR__).'/../../vendor/autoload.php')) {
    $autoloadPathDbconn= realpath(__DIR__).'/../../vendor/autoload.php';

    require_once($autoloadPathDbconn) ;

    $dotenv = Dotenv\Dotenv::createUnsafeImmutable(realpath(__DIR__).'/../../');
    $dotenv->load();

}

define("BX_USE_MYSQLI", true);
define("DBPersistent", false);
.......


But something seems to me that declaring a container here and clogging it with services is generally a bad idea
$container = new Container();
// define some services
$container['session_storage'] = function ($c) {
    return new SessionStorage('SESSION_ID');
};


In general, I want to "correctly" find the place and way to initialize the container and then use it in all my code (components, services)

How about init.php?
in some event like:
AddEventHandler("main", "OnBeforeProlog", array("Page", "OnBeforePrologHandler"));


those before loading the prologue, I will take the environment variables I need, I will pull something from the cookie \ session according to simple logic, I will configure my container, add it to a global variable, and I will pull from the components and other files
But the explicit use of global variables is something for me don't like it at all
global $container;
.....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Emelyanov, 2020-11-30
@babarun

Available from version main 20.5.400
Service Locator https://dev.1c-bitrix.ru/learning/course/index.php...
Locator registration https://dev.1c-bitrix.ru/learning/course/index .php...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question