0
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mercure\PublisherInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Category;
use Symfony\Contracts\Translation\TranslatorInterface;


    /**
     * @Route("/push", name="push")
     * @param Request $request
     * @param PublisherInterface $publisher
     * @return Response
     */
    public function push(Request $request, PublisherInterface $publisher): Response
    {
        $update = new Update(
            '/chat',
            json_encode(['message' => 'Hello World!'])
        );

        $publisher($update);
        return new JsonResponse($publisher);
    }

I get this error:

Cannot autowire argument $publisher of "App\Controller\MainController::push()": it references interface "Symfony\Component\Mercure\PublisherInterface" but no such service exists. Did you create a class that implements this interface?

/*
 * This file is part of the Mercure Component project.
 *
 * (c) Kévin Dunglas <dunglas@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Symfony\Component\Mercure;

/**
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
 *
 * @experimental
 */
interface PublisherInterface
{
    public function __invoke(Update $update): string;
}

Why is this happening? The class is already there. I followed Symfony's official docs and saw some tutorials and they don't seem to have this issue. Do you have any idea of what the problem could be there? Thanks!

Lucifer
  • 21
  • 3
  • What is your answer to the question that Symfony asked you inside the error message: `Did you create a class that implements this interface?` – El_Vanja Feb 05 '21 at 14:39
  • Do you have autowiring (https://symfony.com/doc/current/service_container/autowiring.html) enabled for your controller? Open your `services.yml` and check that `autowire` is set to true, that your controller is in `./src/Controller/` folder and that in services.yml you have this: `App\Controller\: resource: '../src/Controller/' tags: ['controller.service_arguments']` – PierrickM Feb 05 '21 at 14:50

3 Answers3

0

Make sure you have the bundle enabled and configured it accordingly:

In example:

mercure:
    hubs:
        default:
            url: '%env(MERCURE_PUBLISH_URL)%'
            jwt: '%env(MERCURE_JWT_SECRET)%'

https://github.com/stefpe/symfony_mercure/blob/master/config/packages/mercure.yaml

To check your results use debug:config MercureBundle or debug:container | grep mercure.

ju_
  • 569
  • 1
  • 4
  • 17
0

I advise you not to use the PublisherInterface but use HubInterface instead because the PublisherInterface class is now deprecated.

Here is an example:

public function myFunction(HubInterface $hub){

 $update = new Update("mytopic/23", ["message" => "myMessage"]);
 $hub->publish($update);

 return $this->json(["message" => "ok"]);

}
Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
Stollpy
  • 35
  • 6
0

I'm trying to inject the HubInterface as a dependency into a Command in Symfony. arguments: [ '@mercure.hub.default.traceable' ] This works fine in dev. but it doesn't work in prod.

I'm really clueless and already filed an issue with the Symfony/Mercure bundle on Github. Do you guys know anything about it? It should work tho...

Rickerd
  • 104
  • 9