Loading Service Definitions From Within a Bundle
If you want to load services from your bundle instead of having to define them in
config/services.yaml you need to
create a dependency injection extension which is able to load your service definitions. You can find detailed documentation
on this topic here: Extensions Documentation.
As an example, we want to create an extension for the
App which is able to load a
First, we need to create an extension class. The extension class name follows the convention that it is located in
DependencyInjection sub-namespace and that it is named the same as the bundle class, but with
Bundle replaced with
Extension. So for your
MyBundle, we'll create the following extension:
class AppExtension extends Extension
public function load(array $configs, ContainerBuilder $container): void
// create a YamlFileLoader - this could also be a XmlFileLoader if you want to load XML
$loader = new YamlFileLoader(
// looks in src/MyBundle/Resources/config
new FileLocator(__DIR__ . '/../Resources/config')
// load services.yaml
// more load() calls as needed...
Next, we create the config file we're trying to load:
services.yml should now automatically be loaded and register the
my_custom_class service on the container.