pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

    <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 class="eeuyaie" 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 注入服務(wù),我如何直接從

        How do I get a service from the container directly, if I didn#39;t/couldn#39;t inject the service using DI?(如果我沒有/無法使用 DI 注入服務(wù),我如何直接從容器中獲取服務(wù)?)

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

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

                <tbody id='k5fbp'></tbody>

              <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 class="yok0e0c" id='k5fbp'><tfoot id='k5fbp'></tfoot><dl id='k5fbp'><fieldset id='k5fbp'></fieldset></dl></div>
                • 本文介紹了如果我沒有/無法使用 DI 注入服務(wù),我如何直接從容器中獲取服務(wù)?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有一部分代碼,我通過依賴注入注入了兩個服務(wù) $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'
                  

                  但由于某些原因我想通過方法注入服務(wù):

                  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.

                  我收到一個錯誤:

                  未找到服務(wù)checker":事件雖然它存在于應(yīng)用程序的容器中,但AppControllerDefaultController"中的容器是一個較小的服務(wù)定位器,它只知道http_kernel"、parameter_bag"、request_stack"、路由器"、會話"和樹枝"服務(wù).嘗試改用依賴注入.

                  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?

                  推薦答案

                  你需要添加你的依賴項(xiàng) 服務(wù)定位器可以找到它們.

                  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,
                          ]
                      );
                  }
                  

                  如果你的控制器類擴(kuò)展了 AbstractController 你可以簡單地做:

                  If your controller class extends AbstractController you can simply do:

                  $this->get('checker');
                  

                  如果您想在其他類型的類中執(zhí)行此操作(例如不擴(kuò)展AbstractController 的服務(wù)),那么您需要聲明您的服務(wù)實(shí)現(xiàn)了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.

                  這篇關(guān)于如果我沒有/無法使用 DI 注入服務(wù),我如何直接從容器中獲取服務(wù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標(biāo)不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術(shù)方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動程序)

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

                    <bdo id='Z5ES9'></bdo><ul id='Z5ES9'></ul>
                      <tbody id='Z5ES9'></tbody>
                      <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 class="8umsuku" 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>
                          • 主站蜘蛛池模板: 派财经_聚焦数字经济内容服务平台 | 达利园物流科技集团- | 拖鞋定制厂家-品牌拖鞋代加工厂-振扬实业中国高端拖鞋大型制造商 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 中视电广_短视频拍摄_短视频推广_短视频代运营_宣传片拍摄_影视广告制作_中视电广 | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 过滤器_自清洗过滤器_气体过滤器_苏州华凯过滤技术有限公司 | 罗茨真空机组,立式无油往复真空泵,2BV水环真空泵-力侨真空科技 | 重庆监控_电子围栏设备安装公司_门禁停车场管理系统-劲浪科技公司 | 橡胶粉碎机_橡胶磨粉机_轮胎粉碎机_轮胎磨粉机-河南鼎聚重工机械制造有限公司 | 冷柜风机-冰柜电机-罩极电机-外转子风机-EC直流电机厂家-杭州金久电器有限公司 | 齿轮减速机_齿轮减速电机-VEMT蜗轮蜗杆减速机马达生产厂家瓦玛特传动瑞环机电 | 安徽千住锡膏_安徽阿尔法锡膏锡条_安徽唯特偶锡膏_卡夫特胶水-芜湖荣亮电子科技有限公司 | 除尘器布袋骨架,除尘器滤袋,除尘器骨架,电磁脉冲阀膜片,卸灰阀,螺旋输送机-泊头市天润环保机械设备有限公司 | 佛山商标注册_商标注册代理|专利注册申请_商标注册公司_鸿邦知识产权 | 电抗器-能曼电气-电抗器专业制造商| 苏州柯瑞德货架-仓库自动化改造解决方案 | CTAB,表面活性剂1631溴型(十六烷基三甲基溴化铵)-上海升纬化工原料有限公司 | 气动|电动调节阀|球阀|蝶阀-自力式调节阀-上海渠工阀门管道工程有限公司 | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 钢托盘,铁托盘,钢制托盘,镀锌托盘,饲料托盘,钢托盘制造商-南京飞天金属13260753852 | 高压分散机(高压细胞破碎仪)百科-北京天恩瀚拓 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 上海噪音治理公司-专业隔音降噪公司-中广通环保 | 苏州柯瑞德货架-仓库自动化改造解决方案 | 烟台螺纹,烟台H型钢,烟台钢材,烟台角钢-烟台市正丰金属材料有限公司 | [官网]叛逆孩子管教_戒网瘾学校_全封闭问题青少年素质教育_新起点青少年特训学校 | vr安全体验馆|交通安全|工地安全|禁毒|消防|安全教育体验馆|安全体验教室-贝森德(深圳)科技 | 定量包装机,颗粒定量包装机,粉剂定量包装机,背封颗粒包装机,定量灌装机-上海铸衡电子科技有限公司 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 苏州教学设备-化工教学设备-环境工程教学模型|同科教仪 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 千斤顶,液压千斤顶-力良企业,专业的液压千斤顶制造商,shliliang.com | 无锡不干胶标签,卷筒标签,无锡瑞彩包装材料有限公司 | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 金环宇|金环宇电线|金环宇电缆|金环宇电线电缆|深圳市金环宇电线电缆有限公司|金环宇电缆集团 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | RFID电子标签厂家-上海尼太普电子有限公司 | 山东限矩型液力偶合器_液力耦合器易熔塞厂家-淄博市汇川源机械厂 | 杭州翻译公司_驾照翻译_专业人工翻译-杭州以琳翻译有限公司官网 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 |