Providing Services from a Syfony bundle

Fremantle

I'm trying to add a service in a redistributable Symfony 4 bundle. The docs say it is just a matter of loading the service configuration in the bundle's Extension class. For example, for the GoatBundle:

class GoatExtension extends \Symfony\Component\DependencyInjection\Extension\Extension {
    public function load(array $configs, ContainerBuilder $container) {
        $configDir = dirname(__DIR__).'/Resources/config';
        $loader = new YamlFileLoader($container, new FileLocator($configDir));
        $loader->load('services.yml');
    }
}

Where GoatBundle/Resources/config/services.yml looks like this:

Name\Of\InjectedClass:
  factory: [ '\Factory\For\InjectedClass', serviceFactory ]
  arguments:
    - '@service_container'

But this results in:

There is no extension able to load the configuration for "Name\Of\InjectedClass" (in GoatBundle/Resources/config/services.yml). Looked for namespace "Name\Of\InjectedClass", found none

I went around and around in circles until I realised that I was simply missing the top-level services key in services.yml! It needs to be like this:

services:
  Name\Of\InjectedClass:
    factory: [ '\Factory\For\InjectedClass', serviceFactory ]
    arguments:
      - '@service_container'

I'm only writing it all down here because it was only by this rubber ducking that I saw the problem.

Comments on this blog post+ Add a comment
No comments yet