如果我没有/不能使用DI注入服务,如何直接从容器中获得服务? [英] How do I get a service from the container directly, if I didn't/couldn't inject the service using DI?

查看:37
本文介绍了如果我没有/不能使用DI注入服务,如何直接从容器中获得服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一部分代码通过依赖注入注入了两个服务 $ checker $ paginator .效果很好:

I have a part of code where I'm injecting two services $checker and $paginator by dependency injection. It works perfectly:

public function index(Request $request, Paginator $paginator, Checker $checker)
    {
        $result = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
        $partialResult = $paginator->getPartial($result, 0, 3);
        $checker->isValid('A');
        var_dump("test");
        die;
    }

services.yaml 文件上的配置下面:

   paginator:
        public: true
        class: 'App\Helper\Paginator'

    checker:
        public: true
        class: 'App\Helper\Checker'
        arguments:
         $paginator: '@paginator'

但是出于某些原因,我想通过方法注入服务:

But I'd like to inject for some reasons service by method:

$checker = $this->container->get('checker');

但是它不起作用.在以前的版本中,Symfony就像3.4一样.

But it doesn't work. In previous versions Symfony like 3.4 it used to.

我收到一个错误:

未找到服务检查器":事件虽然存在于应用程序的容器中,但"App \ Controller \ DefaultController"中的容器是一个较小的服务定位器,仅了解"http_kernel","parameter_bag","request_stack",路由器",会话"和树枝"服务.尝试改用依赖项注入.

Service "checker" not found: event though it exists in the app's container, the container inside "App\Controller\DefaultController" is a smaller service locator that only knows about the "http_kernel", "parameter_bag", "request_stack", "router", "session", and "twig" services. Try using dependency injection instead.

我该如何解决?

推荐答案

您需要添加依赖项,以便

You need to add your dependencies so the service locator can find them.

向控制器类添加方法 getSubscribedServices():

public static function getSubscribedServices()
{
    return array_merge(
        parent::getSubscribedServices(),
        [
            'checker' => Checker::class,
        ]
    );
}

如果您的控制器类扩展了 AbstractController ,则只需执行以下操作:

If your controller class extends AbstractController you can simply do:

$this->get('checker');

如果要在其他类型的类中进行此操作(例如,不扩展 AbstractController 的服务),则需要声明您的服务实现了 ServiceSubscriberInterface .

If you want to do it in another type of class (e.g. a service that doesn't extend AbstractController), then you need to declare that your service implements the ServiceSubscriberInterface.

use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Psr\Container\ContainerInterface;

class FooService implements ServiceSubscriberInterface {

    public function __construct(ContainerInterface $locator)
    {
        $this->locator = $locator;
    }

    public static function getSubscribedServices() { /* same as before */ }

    public function get($service)
    {
        return $this->locator->get($service);
    }
}

...,您将能够比之前的控制器做相同的事情.

... and you would be able to do the same than you in the controller earlier.

这篇关于如果我没有/不能使用DI注入服务,如何直接从容器中获得服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆