'Symfony registering services with ContainerControllerResolver
I am trying to use the Symfony dependency injection component and I was just wondering if anyone would be able to help with the registering of the services.
I have recently moved to ContainerControllerResolver opposed to ControllerResolver so that my routes correctly instantiate the dependencies.
This is working well enough however I have ran into an issue when a class is needed more than once.
So for ContainerControllerResolver to work you seem to have to use the full class path. Where as when you use ControllerResolver you can set your own unique string ID.
I can't see anyway to set a unique ID when using ContainerControllerResolver and you don't seem able to pass arguments to the Reference class.
Is there anyway I could rewrite the below so they are seperate instances? The ControllerResolver way of being able to set unique IDs makes sense to me but I'm just a little lost when you have to pass the full class.
$containerBuilder->register(App\Models\Database::class, App\Models\Database::class)->setArguments([
DB1, DB1USER, DB1PASS
]);
$containerBuilder->register(App\Models\News::class, App\Models\News::class)->setArguments([
new Reference(App\Models\Database::class)
]);
$containerBuilder->register(App\Models\Database::class, App\Models\Database::class)->setArguments([
DB2, DB2USER, DB2PASS
]);
$containerBuilder->register(App\Models\Reports::class, App\Models\Reports::class)->setArguments([
new Reference(App\Models\Database::class)
]);
Solution 1:[1]
I think I got the wrong gist of things from another post. They mentioned it is more ideal to use the controller name in the ID when using the ContainerControllerResolver. I've been able to use service IDs in the first param of the register and I've just updated my routes _controller param to go to that same name rather than the class namespace path:
$routes = new RouteCollection();
$routes->add('news', new Route('/news', [
'_controller' => 'controller.news::newsOutput',
]));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mmakes |
