I'm refactoring old application in PHP.
I'm trying to use Symphony dependency injection component to inject services into controllers (or other services), but I don't know how to achieve this, because symphony documentation is more prepared for using framework, than framework components.
I already have my own Kernel, Container that contains all services and controllers (controllers are registered as services already). My controllers extending AbstractController from symfony/frameworkbundle. So the only thing I can do now is:
Get service from container by $this->container->get('service_id'), but if service in constructor will have class as parameter
public function __constructor(SomeClass $someClass)
then I'm getting this exception:
The "App\V2\Service\TestService" 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.
If I change configuration to make all services public, then:
Too few arguments to function APP\V2\Service\TestService::__construct(), 0 passed and exactly 1 expected
I prepare a gist, to have better view what I'm talking about: https://gist.github.com/miedzwin/49bac1cc1d5270d3ba1bfcf700abf864
Can someone help me a bit with DI implementation using Symfony components (not Symfony framework)? Good working example would be enough. Or just please put your remarks to my gist, I try to fix this.
APP\V2\Service\T estServicelook like? Where do you register it? - Tomas Votrubaautowire: true. I updated the gist, so you can check how TestService.php looks. - miedzwinpublic: true, then services that have in constructor scalar type arguments (string, int, array) cannot be autowired.Cannot autowire service "APP\V2\Service\API": argument "$facebookUserId" of method "__construct()" has no type-hint, you should configure its value explicitly.- miedzwinpublic: false-Controller "APP\V2\Controller\API\SocialNetworkController::loginAction()" requires that you provide a value for the "$testService" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.So, looks like service still isn't injected. - miedzwin