依赖项注入无效,但是是手动定义的

时间:2018-10-05 13:55:04

标签: php symfony service dependency-injection

我是Symfony的新手,并尝试使用gos / web-socket-bundle中的ClientManipulatorInterface服务。 我的问题是,即使我手动配置了参数,Symfony也会返回错误。

我总是收到此错误:无法自动装配服务“ Foo \ Bar \ Controller \ testTopic”:方法“ __construct()”的参数“ $ clientManipulator”引用接口“ Gos \ Bundle \ WebSocketBundle \ Client \ ClientManipulatorInterface”,但没有这样的接口服务存在。你应该
  将该接口别名为现有的“ gos_web_socket.websocket.client_manipulator”服务。

这是我的服务。yaml:

services:
    test_topic:
        class: Foo\Bar\Controller\testTopic
        tags:
            - { name: gos_web_socket.topic }
        arguments:
            - '@gos_web_socket.websocket.client_manipulator'

这是我的PHP类:

namespace Foo\Bar\Controller;

use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;

class testTopic implements TopicInterface {

    /**
     * @var ClientManipulatorInterface
     */
    protected $clientManipulator;

    /**
     * testTopic constructor.
     * @param ClientManipulatorInterface $clientManipulator
     */
    public function __construct(ClientManipulatorInterface $clientManipulator) {
        $this->clientManipulator = $clientManipulator;
    }

    ...

1 个答案:

答案 0 :(得分:1)

您需要将ClientManipulatorInterface别名为Symfony的一种实现,以便能够正确自动关联依赖项:

services:
    Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface: '@gos_web_socket.websocket.client_manipulator'

    Foo\Bar\Controller\testTopic:
        autowire: true
        tags:
            - { name: gos_web_socket.topic }

之后清除缓存!