1

Using DI as a standalone component in the small part of the codebase I want to make some services visible outside this part and accessible as by interface class name.

As I know, I should use a container for this. So when old part of the code (that can't use DI) wants to use service that implementation is configured by this part of the code: it should call $container->get(MyInterface:class).

The problem is that I receive:

The "App\MyInterface" service or alias has been removed or inlined when the container was compiled. You should either make it public or stop using the container directly and use dependency injection instead.

I can't use DI in other parts of the code. So I want to make some of my services public. This is my code:

services.yaml:

# ...
# autowire & autoconfigure: true
# catalogs included

App\MyInterface: '@my.configured.implementation'

my.configured.implementation:
    class: App\MyInterfaceImplementation

Builder:

$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__));
$loader->load('services.yaml');

//Make `MyInterface::class` public so
//$container->get(MyInterface::class) should work.
$containerBuilder->getDefinition(MyInterface::class)->setPublic(true);

$containerBuilder->compile(true);

When it comes to getting a definition I receive:

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "App\MyInterface"

How can I make this works? Or maybe there is better way to use one service by these two modules?

  • Have you tried adding public:true to your service definition? – Cerad May 20 '19 at 13:07
  • @Cerad Hi, thanks for response. It will make `my.configured.implementation` public, not `App\MyInterface`. So `$container->get('App\MyInterface')` ends with `ServiceNotFoundException`. – Sebastian Tkaczyk May 20 '19 at 13:19
  • 1
    Add it to your alias definition. Aliases do not have to be defined in one line. You can use multiple lines and add the public:true to it. https://symfony.com/doc/current/service_container/alias_private.html#aliasing – Cerad May 20 '19 at 13:30
  • @Cerad When I added `alias: @my.configured.implementation` and `public:true` to `App\MyInterface`. I got `my.configured.implementation` is non-existent service. even when i set it to public. :/ – Sebastian Tkaczyk May 20 '19 at 13:42
  • Oh... I removed `@` from alias and its works. But I'm not sure why. – Sebastian Tkaczyk May 20 '19 at 13:47
  • 1
    It works without @ because the second argument to an alias is always a service. So the @ would be redundant and was removed. However, aliases used to work with or without an @ so sometimes you might run across old examples with an @. And while off-topic, unless you have a good reason to use something like 'my.configured.implementation' as the service name, you should probably just use the class name. Again, you might see old examples like this but the class name is more or less the current standard. – Cerad May 20 '19 at 14:10

0 Answers0