Man*_*mar 2 php symfony symfony-2.6
我在symfony 2.6中按名称进行了服务设置,ge_lib如下所示
ge_lib:
class: GE\RestBundle\Services\GELib
arguments: [@session, @doctrine.orm.entity_manager, @manage_ge_proc]
Run Code Online (Sandbox Code Playgroud)
在GELib.php中我需要使用来自其他服务的函数 manage_ge_proc
manage_ge_proc:
class: GE\RestBundle\Services\GEManageProcedure
arguments: [@doctrine.orm.entity_manager, @manage_ge_native_query]
Run Code Online (Sandbox Code Playgroud)
如果我尝试以这种方式使用它,它就不起作用了
$emailInv = $this->get('manage_ge_proc');
$sendStatus = $emailInv->pSendGeneralEmail(.....);
Run Code Online (Sandbox Code Playgroud)
它给出错误,说无法通过该名称找到任何get函数.通常这 - > $this->get('manage_ge_proc');在任何控制器中工作.但我如何在服务中使用它?
我试过$this->getContainer()->get('manage_ge_proc');但它没用.
此调用是从DI容器获取服务,您在服务中没有这个服务
$this->get('manage_ge_proc');
Run Code Online (Sandbox Code Playgroud)
它在控制器中工作,因为DI容器会自动注入其中.
既然你有这一行services.yml,它告诉Symfony将@manage_de_proc服务注入ge_lib构造函数
arguments: [@session, @doctrine.orm.entity_manager, @manage_ge_proc]
Run Code Online (Sandbox Code Playgroud)
你应该能够@manage_de_proc从这样的构造函数中选择:
public function __construct(
Session $session,
EntityManager $entityManager,
GEManageProcedure $manageDeProc
)
{
//... whatever you do in your constructor
$this->manageDeProc = $manageDeProc;
}
Run Code Online (Sandbox Code Playgroud)