php - how to inject a service in to a command that uses another service inside? -


i kind of new symfony2.

i have service controller called curlcontroller, controller have called mycontroller uses curl controller service create curl requests. use $this->getcontainer()->get('cont.my') call it.

now want add symfony command vars command line , passes mycontroller, wrote mycontroller service (cont.my) , call command using $this->container->get('cont.my').

it works command , in mycontroller functions, problem service inside doesnt work more.

what doing wrong?

the error receive :

php fatal error:  call member function get() on non-object 

service.yml:

services: utils.curl:     class: project\commonbundle\controller\utilscurlcontroller cont.my:         class: project\serverbundle\controller\mycontroller 

mycommand :

class mycommand extends containerawarecommand{ protected function configure(){     $this->setname('mycommand:execute')          ->setdescription('create pull request'); }  /**  * {@inheritdoc}  */ protected function execute(inputinterface $input, outputinterface $output){      $serverresponse = $this->getcontainer()->get('cont.my');     $output->writeln(sprintf('<comment>'.$serverresponse->printhello().'</comment>')); } 

}

mycontroller:

class mycontroller extends controller{  public function printhello(){     $serverresponse = $this->container->get('utils.curl')         ->seturl('example.com')         ->createcurl();     return $serverresponse; } 

}

edit :

if fix adding :

calls:           - [ setcontainer, [ @service_container ] ] 

to services.yml. why did did trick dont it.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -