dpbdl44228 2017-08-08 10:36
浏览 41
已采纳

symfony v3.3.6意外的服务行为

Im working on a Symfony3 project and playing around with the "new" dependency injection mechanism. can some please explain the following behavior to me please:

this is my setup:


services.yml

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  MyBundle\Controller\:
    resource: '../../Controller/'
    public: true
    tags: ['controller.service_arguments']

  MyBundle\A: 
    public:false

Controller Scenario A

<?php

namespace MyBundle\Controller;

use ...

class RootController extends Controller
{
    /**
     * @Route("/", name="root")
     */
    public function indexAction(A $a) <- INJECTION HERE
    {
        $b = $this->get(A::class); // NO EXCEPTION THROWN HERE

        return new Response(); 
    }
}

Controller Scenario B

<?php

namespace MyBundle\Controller;

use ...

class RootController extends Controller
{
    /**
     * @Route("/", name="root")
     */
    public function indexAction() // <- NO INJECTION
    {
        $b = $this->get(A::class); // EXCEPTION THROWN HERE

        return new Response(); 
    }
}

Im trying to fetch service A inside the indexAction from the container. As I marked it private in my services.yml I'm expecting an Exception when trying to grab it (like in Scenario B). But in Scenario A I'm not getting an Exception because the service A has already been injected (auto wired) into the Controller.

(here the (expected) Exception of Scenario B: You have requested a non-existent service "MyBundle\A".)

Can some explain this behaviour please.

cheers

  • 写回答

2条回答 默认 最新

  • drmticpet66231422 2018-05-18 19:45
    关注

    After bit of explaining in the comments I understand your question as:

    How does a private service becomes public by request in the controller action?

    With action injection these services collected are re-registred with ServiceLocator class, which make it accessible with ->get().

    See RegisterControllerArgumentLocatorsPass here.

    enter image description here

    I agree it's a bit unclear behavior. Not all paths can be covered I guess.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突