oivlis111
oivlis111

Reputation: 55

Dependency Injection not working, but defined manually

I'm new to Symfony and try to use the ClientManipulatorInterface service from gos/web-socket-bundle. My problem is that Symfony returns an error even if I configure the argument manually.

I always get this error: Cannot autowire service "Foo\Bar\Controller\testTopic": argument "$clientManipulator" of method "__construct()" references interface "Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface" but no such service exists. You should maybe
alias this interface to the existing "gos_web_socket.websocket.client_manipulator" service.

Here is my service.yaml:

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

This is my PHP class:

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;
    }

    ...

Upvotes: 1

Views: 437

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52513

You need to alias the ClientManipulatorInterface to one of it's implementations for Symfony to be able to autowire the dependency correctly:

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

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

Clear your cache afterwards!

Upvotes: 1

Related Questions