Developer
Developer

Reputation: 2895

Symfony 4 service alias

I have service in bundle

@sonata.page.twig.extension

it is private.

I need set is as public.

Try 1.

  Sonata\PageBundle\Twig\Extension\PageExtension:
    alias: "@sonata.page.twig.extension"
    public: true

get

 InvalidArgumentException
Unable to replace alias "Sonata\PageBundle\Twig\Extension\PageExtension" with actual definition "@sonata.page.twig.extension".

Try 2

mea.sonata.page.twig.extension:
    alias: "@sonata.page.twig.extension"
    public: true

same error

Upvotes: 1

Views: 1292

Answers (2)

Elbarto
Elbarto

Reputation: 1253

To clarify the use of '@' in this example : "@sonata.page.twig.extension"

Without it, you are telling the container to pass the string sonata.page.twig.extension.
With it, you are telling the container to pass the service sonata.page.twig.extension.

From the documentation, see the following comment in the code : https://symfony.com/doc/current/service_container.html

 # explicitly configure the service
    App\Service\MessageGenerator:
        arguments:
            # the '@' symbol is important: that's what tells the container
            # you want to pass the *service* whose id is 'monolog.logger.request',
            # and not just the *string* 'monolog.logger.request'
            $logger: '@monolog.logger.request'

Upvotes: 1

Developer
Developer

Reputation: 2895

OK found solution

  Sonata\PageBundle\Twig\Extension\PageExtension:
    alias: 'sonata.page.twig.extension'
    public: true

Why sometimes @sonata.page.twig.extension sometimes sonata.page.twig.extension

Upvotes: 0

Related Questions