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

我收到此错误:

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

为什么会这样?课已经在那里了。我关注了 Symfony 的官方文档,看了一些教程,他们似乎没有这个问题。您知道可能存在什么问题吗?谢谢!

4

3 回答 3

0

我正在尝试将 HubInterface 作为依赖项注入到 Symfony 中的命令中。参数:['@mercure.hub.default.traceable'] 这在开发中运行良好。但它在产品中不起作用。

我真的一无所知,并且已经在 Github 上提交了 Symfony/Mercure 捆绑包的问题。你们知道吗?它应该可以工作...

于 2021-12-20T21:20:46.933 回答
0

确保您启用了捆绑包并进行了相应的配置:

例如:

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

要检查您的结果,请使用debug:config MercureBundledebug:container | grep mercure

于 2021-03-25T10:47:38.220 回答
0

我建议您不要使用,PublisherInterface而是使用HubInterface,因为PublisherInterface该类现在已被弃用。

这是一个例子:

public function myFunction(HubInterface $hub){

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

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

}
于 2021-06-18T08:55:56.400 回答