php 如何在 Symfony 的另一个服务中注入一个服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11801541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inject a service in another service in Symfony?
提问by jini
I am trying to use the logging service in another service in order to trouble shoot that service.
我正在尝试在另一个服务中使用日志服务,以便对该服务进行故障排除。
My config.yml looks like this:
我的 config.yml 看起来像这样:
services:
userbundle_service:
class: Main\UserBundle\Controller\UserBundleService
arguments: [@security.context]
log_handler:
class: %monolog.handler.stream.class%
arguments: [ %kernel.logs_dir%/%kernel.environment%.jini.log ]
logger:
class: %monolog.logger.class%
arguments: [ jini ]
calls: [ [pushHandler, [@log_handler]] ]
This works fine in controllers etc. however I get no out put when I use it in other services.
这在控制器等中工作正常。但是当我在其他服务中使用它时我没有输出。
Any tips?
有小费吗?
回答by Inoryy
You pass service id as argument to constructor or setter of a service.
您将服务 ID 作为参数传递给服务的构造函数或设置器。
Assuming your other service is the userbundle_service:
假设您的其他服务是userbundle_service:
userbundle_service:
class: Main\UserBundle\Controller\UserBundleService
arguments: [@security.context, @logger]
Now Logger is passed to UserBundleServiceconstructor provided you properly update it, e.G.
现在 Logger 被传递给UserBundleService构造函数,前提是您正确更新它,eG
protected $securityContext;
protected $logger;
public function __construct(SecurityContextInterface $securityContext, Logger $logger)
{
$this->securityContext = $securityContext;
$this->logger = $logger;
}
回答by Niket Pathak
For Symfony 3.3, 4.x and above, the easiest solution is to use Dependency Injection
对于 Symfony 3.3、4.x 及更高版本,最简单的解决方案是使用依赖注入
You can directly inject the service into another service, (say MainService)
你可以直接将服务注入到另一个服务中,(比如MainService)
// AppBundle/Services/MainService.php
// 'serviceName' is the service we want to inject
public function __construct(\AppBundle\Services\serviceName $injectedService) {
$this->injectedService = $injectedService;
}
Then simply, use the injected service in any method of the MainService as
然后简单地,在 MainService 的任何方法中使用注入的服务作为
// AppBundle/Services/MainService.php
public function mainServiceMethod() {
$this->injectedService->doSomething();
}
And viola! You can access any function of the Injected Service!
还有中提琴!您可以访问注入服务的任何功能!
For older versions of Symfony where autowiring does not exist -
对于不存在自动装配的旧版 Symfony -
// services.yml
services:
\AppBundle\Services\MainService:
arguments: ['@injectedService']
回答by user7867717
You can use the container in your service :
您可以在服务中使用容器:
userbundle_service:
class: Main\UserBundle\Controller\UserBundleService
arguments: [@security.context]
In your service :
为您服务:
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserBundleService {
/**
* @var ContainerInterface
*/
private $_container;
public function __construct(ContainerInterface $_container) {
$this->_container = $_container;
}
$var = $this->_container->get('logger');
$var->functionLoggerService();
}

