Symfony Mercure Error SymfonyComponentMercurePublisherInt


use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentMercurePublisherInterface;
use SymfonyComponentMercureUpdate;
use SymfonyComponentRoutingAnnotationRoute;
use AppEntityCategory;
use SymfonyContractsTranslationTranslatorInterface;

/**
* @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 "AppControllerMainController::push()": it references interface "SymfonyComponentMercurePublisherInterface" 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 SymfonyComponentMercure;
/**
* @author Vincent Chalamon <vincentchalamon@gmail.com>
*
* @experimental
*/
interface PublisherInterface
{
public function __invoke(Update $update): string;
}

为什么会发生这种情况?班级已经在那里了。我跟随Symfony的官方文档,看到了一些教程,他们似乎没有这个问题。你知道会有什么问题吗?谢谢!

确保您已经启用了bundle并相应地配置了它:

在例子:

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

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

下面是一个例子:

public function myFunction(HubInterface $hub){
$update = new Update("mytopic/23", ["message" => "myMessage"]);
$hub->publish($update);
return $this->json(["message" => "ok"]);
}

我试图将HubInterface作为依赖项注入Symfony中的命令。参数:['@ mercurure .hub.default.traceable']这在dev中工作得很好,但在prod中不起作用。

我真的很无能,并且已经在Github上提交了Symfony/mercury bundle的问题。你们知道这件事吗?

最新更新