0
votes

I'm trying something so much time but maybe it's not even possible. Sorry for my bad language. So, I followed Symfony Doc https://symfony.com/doc/current/bundles.html to create new Bundle, and than I followed https://symfony.com/doc/current/bundles/extension.html to create DI extension.

My files: AcmeHelloExtension.php

namespace App\Acme\HelloBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AcmeHelloExtension extends Extension
{

    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new XmlFileLoader(
            $container,
            new FileLocator(__DIR__.'/../Resources/config')
        );
        $loader->load('services.xml');
    }
}

AcmeHelloBundle.php


namespace App\Acme\HelloBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeHelloBundle extends Bundle
{

}

I added it to config/bundles.php

src/Acme/HelloBundle/Resources/config/services.yaml

services:
    App\Acme\HelloBundle\AcmeHelloBundle:
        tags: ['example-tags']

This service file isn't auto loaded, do I need to do next steps or it should work? When I check it with debug:container ....bundle... Option-Tags has empty value. When I put this code in config/services.yaml it works.

1
Why are you trying to make the bundle class a service? Also, the code you posted tries to load services.xml but in your question you refer to it as services.yaml. Typo? In any event, stick a die statement in Extension::load and confirm it is being called.Cerad
How do you mean why? I want to configure services inside my bundle. Oh sry, doesn't matter xml file or yaml locator... die statement is not called.tila98
Did you actually call your bundle AcmeHello? I ask because the bundle and the extension prefix must be the same in order for the extension to be called. So if you used something different then check that they match. Maybe post the line from config/bundles.php so we can check the environment portion. As far as the AcmeHelloBundle service entry goes, maybe you added it as an example but there is not need to define a service for it. Other classes yes, bundles classes no.Cerad
If you are still having trouble then create a new project, add a simple bundle and post to github. There are several pieces that need to be properly setup for everything to work. It will be easy to find the problem with a complete example. You might even find the problem yourself during the process.Cerad
@Cerad App\Acme\HelloBundle\AcmeHelloBundle::class => ['all' => true] Now, it load service file, I tried it with die statement. But it does not want to change my service config. Ok, I will make new project and post github link here. Just few min...tila98

1 Answers

0
votes

The basic problem was that the bundle's source code was located under the project's src directory:

project
    src
        Ztest
            ZtestBundle.php

This in turn was causing the bundle's services to be be autowired by the application's config/services.yaml file:

# project\config\services
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            ...
            - '../src/Ztest' # Add this to fix the problem

Excluding the bundle's source code fixed the issues. Autowiring at the application level overrides any manual wiring done at the bundle level.

Of course in general if you do decide you need a bundle then it's source code should be in it's own independent directory:

project
    src
    src-ztest-bundle

For this to work you also need to update the psr-4 section of composer.json and run "composer dump-autoload".

Keep in mind that in Symfony 4+, the only recommended usage of custom bundles is for code shared across multiple Symfony applications. In which case, the bundle should ultimately end up in it's own repository and composer package.

However, custom bundles inside of an application are still supported and there are times where they come in useful.