<bdo id='k5fbp'></bdo><ul id='k5fbp'></ul>

    1. <tfoot id='k5fbp'></tfoot>
    2. <small id='k5fbp'></small><noframes id='k5fbp'>

      <legend id='k5fbp'><style id='k5fbp'><dir id='k5fbp'><q id='k5fbp'></q></dir></style></legend>

      1. <i id='k5fbp'><tr id='k5fbp'><dt id='k5fbp'><q id='k5fbp'><span id='k5fbp'><b id='k5fbp'><form id='k5fbp'><ins id='k5fbp'></ins><ul id='k5fbp'></ul><sub id='k5fbp'></sub></form><legend id='k5fbp'></legend><bdo id='k5fbp'><pre id='k5fbp'><center id='k5fbp'></center></pre></bdo></b><th id='k5fbp'></th></span></q></dt></tr></i><div id='k5fbp'><tfoot id='k5fbp'></tfoot><dl id='k5fbp'><fieldset id='k5fbp'></fieldset></dl></div>
      2. 如果我没有/无法使用 DI 注入服务,我如何直接从

        时间:2023-09-24
          <tfoot id='3DueN'></tfoot>

            <tbody id='3DueN'></tbody>

          <small id='3DueN'></small><noframes id='3DueN'>

            <i id='3DueN'><tr id='3DueN'><dt id='3DueN'><q id='3DueN'><span id='3DueN'><b id='3DueN'><form id='3DueN'><ins id='3DueN'></ins><ul id='3DueN'></ul><sub id='3DueN'></sub></form><legend id='3DueN'></legend><bdo id='3DueN'><pre id='3DueN'><center id='3DueN'></center></pre></bdo></b><th id='3DueN'></th></span></q></dt></tr></i><div id='3DueN'><tfoot id='3DueN'></tfoot><dl id='3DueN'><fieldset id='3DueN'></fieldset></dl></div>
          • <legend id='3DueN'><style id='3DueN'><dir id='3DueN'><q id='3DueN'></q></dir></style></legend>

              • <bdo id='3DueN'></bdo><ul id='3DueN'></ul>
                1. 本文介绍了如果我没有/无法使用 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: 'AppHelperPaginator'
                  
                      checker:
                          public: true
                          class: 'AppHelperChecker'
                          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.

                  我收到一个错误:

                  未找到服务checker":事件虽然它存在于应用程序的容器中,但AppControllerDefaultController"中的容器是一个较小的服务定位器,它只知道http_kernel"、parameter_bag"、request_stack"、路由器"、会话"和树枝"服务.尝试改用依赖注入.

                  Service "checker" not found: event though it exists in the app's container, the container inside "AppControllerDefaultController" 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.

                  我该如何解决这个问题?

                  How should I solve this?

                  推荐答案

                  你需要添加你的依赖项 服务定位器可以找到它们.

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

                  将方法 getSubscribedServices() 添加到您的控制器类:

                  Add a method getSubscribedServices() to your controller class:

                  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 SymfonyContractsServiceServiceSubscriberInterface;
                  use PsrContainerContainerInterface;
                  
                  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 注入服务,我如何直接从容器中获取服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:无法解析的依赖解析 [Parameter #0 [ &lt;required& 下一篇:Laravel Eloquent Query 使用 WHERE 和 OR AND OR?

                  相关文章

                  最新文章

                  <small id='Z5ES9'></small><noframes id='Z5ES9'>

                    <bdo id='Z5ES9'></bdo><ul id='Z5ES9'></ul>
                  1. <tfoot id='Z5ES9'></tfoot>
                    1. <i id='Z5ES9'><tr id='Z5ES9'><dt id='Z5ES9'><q id='Z5ES9'><span id='Z5ES9'><b id='Z5ES9'><form id='Z5ES9'><ins id='Z5ES9'></ins><ul id='Z5ES9'></ul><sub id='Z5ES9'></sub></form><legend id='Z5ES9'></legend><bdo id='Z5ES9'><pre id='Z5ES9'><center id='Z5ES9'></center></pre></bdo></b><th id='Z5ES9'></th></span></q></dt></tr></i><div id='Z5ES9'><tfoot id='Z5ES9'></tfoot><dl id='Z5ES9'><fieldset id='Z5ES9'></fieldset></dl></div>
                      <legend id='Z5ES9'><style id='Z5ES9'><dir id='Z5ES9'><q id='Z5ES9'></q></dir></style></legend>