3

コントローラーにサービスを注入できるようにしたいので、http://symfony.com/doc/current/cookbook/controller/service.htmlを見て、表記法をいじった後 (もう少し一貫性がありますが、何でも) サービス定義エントリを使用して WebTestCase を持っています。

ただし、コントローラーにはコンテナー自体を挿入する必要があり (実際、デフォルトのフレームワーク コントローラーを介して ContainerAware を拡張します)、FrameworkBundle の ControllerResolver はそれを行いません。

コード (Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::createController()) を見ると、これは驚くべきことではありません:

protected function createController($controller)
{
    if (false === strpos($controller, '::')) {
        $count = substr_count($controller, ':');
        if (2 == $count) {
            // controller in the a:b:c notation then
            $controller = $this->parser->parse($controller);
        } elseif (1 == $count) {
            // controller in the service:method notation
            list($service, $method) = explode(':', $controller, 2);

            return array($this->container->get($service), $method);
        } else {
            throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
        }
    }

    list($class, $method) = explode('::', $controller, 2);

    if (!class_exists($class)) {
        throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
    }

    $controller = new $class();
    if ($controller instanceof ContainerAwareInterface) {
        $controller->setContainer($this->container);
    }

    return array($controller, $method);
}

どうやら service:method 表記を使用すると、コンテナ自体を注入するのではなく、コンテナからコントローラを直接返します。

これはバグですか、それとも何か不足していますか?

4

1 に答える 1