Views: 476
Last Modified: 30.04.2021

Service registration

Service registration via the bitrix/.settings.php files

Before querying the service, it must be registered. It can be done via .settings.php files. All the necessary services are listed in the services section.

 [
        'value' => [
            'someServiceName' => [
                'className' => \VendorName\Services\SomeService::class,
            ],                      
            'someGoodServiceName' => [
                'className' => \VendorName\Services\SecondService::class,
                'constructorParams' => ['foo', 'bar'],
            ],                      
        ],
        'readonly' => true,
      ],
      //...
   ];            

As the result, these services will be available immediately after core D7 initialization. Read below about which service description methods are presently available.

$serviceLocator = \Bitrix\Main\DI\ServiceLocator::getInstance();
$someGoodServiceName = $serviceLocator->get('someGoodServiceName');
$someServiceName = $serviceLocator->get('someServiceName');

Service registration using the module settings {moduleName}/.settings.php files

The module root can also have the .settings.php file. You can describe services that belong and are used in this module. Semantics is similar to the description in the global bitrix/.settings.php files and config description rules.

 [
        'value' => [
            'someModule.someServiceName' => [
                'className' => \VendorName\SomeModule\Services\SomeService::class,
            ],                      
            'someModule.someAnotherServiceName' => [
                'constructor' => static function () {
                    return new \VendorName\SomeModule\Services\SecondService('foo', 'bar');
                },
            ],                      
            'someModule.someGoodServiceName' => [
                'className' => \VendorName\SomeModule\Services\SecondService::class,
                'constructorParams' => static function (){
                    return ['foo', 'bar'];
                },
            ],                      
        ],
        'readonly' => true,
      ],
      //...
   ];
Attention! These services will be registered only after module is connected. Also, it is recommended to name the module services by using the module name prefix to avoid issues with service code duplicates, for example:
 iblock.imageUploader
    disk.urlManager
    crm.entityManager
    crm.urlManager
    someModule.urlManager.

Service registration via API

Services can be registered via API as well. Use the class methods \Bitrix\Main\DI\ServiceLocator

Service configuration

Configuration is described as an array and prompts service locator a method for creating an object. Presently, there are three methods for description:

  1. Indicating a service class. Service locator creates a service by calling a new $className.
     'someModule.someServiceName' => [
                'className' => \VendorName\SomeModule\Services\SomeService::class,
            ]
  2. Indicating a service class and parameters to be passed into constructor. Service locator will create a service by calling a new $className('foo', 'bar').
    'someModule.someServiceName' => [
                'className' => \VendorName\SomeModule\Services\SomeService::class,
                'constructorParams' => ['foo', 'bar'],
            ]                      
    
            'someModule.someServiceName' => [
                'className' => \VendorName\SomeModule\Services\SomeService::class,
                'constructorParams' => static function (){
                   return ['foo', 'bar'];
                },
            ]
  3. Indicating a closure-constructor that must create and return a service object.
     'someModule.someAnotherServiceName' => [
                'constructor' => static function () {
                    return new \VendorName\SomeModule\Services\SecondService('foo', 'bar');
                },
            ]



Courses developed by «Bitrix», Inc.