0

DrupalKernel
Building of Dependency Injection Container and registration of Service Providers Core provides the CoreServiceProvider, which, in addition to registering any core services that cannot be registered in the core.services.yaml file, adds any compiler passes needed by core, e.g. for processing tagged services. Each module can add its own service provider, i.e. a class implementing Drupal\Core\DependencyInjection\ServiceProvider, to register services to the container, or modify existing services.

Why cannot all the core services be registered in core.services.yaml?
Why do we need a service provider class to register services or modify existing services, when we can register services in module.services.yml?

  • For #Q3 when you register the service in module.services.yml you need to point your service to a class. That class is your "Service Provider class". Ex in my_module.services.yml class: Drupal\my_module\HelloWorldProvider and my_module.hello_world would be the service in this example. – No Sssweat Nov 22 '19 at 6:28
  • Hi @NoSssweat, but we don't use CoreServiceProvider (which implements ServiceProviderInterface) when defining a custom service, isn't it? – harshal Nov 22 '19 at 6:33
  • That's just mentioning what Core provides that, but then it goes on to say Each module can add its own service provider. Thus, it is not required to use CoreServiceProvider. – No Sssweat Nov 22 '19 at 6:36
  • So CoreServiceProvider has list of services which is registered and is there a way we can alter that list ? or we can only alter the class defined in the CoreServiceProvider and replace it with our own Custom Class ? @NoSssweat – harshal Nov 22 '19 at 6:39
  • 3
    That would be offtopic here, you can find the answer to that here – No Sssweat Nov 22 '19 at 6:54
1

Why cannot all the core services be registered in core.services.yaml?

Why do we need a service provider class to register services or modify existing services, when we can register services in module.services.yml?

I think the answer to both these questions is that likely some of the services are dynamic. Yaml files can only be used for static registration of services.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.