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的官方文档,看了一些教程,他们似乎没有这个问题。你知道问题出在哪里吗?谢谢!
发布于 2021-03-25 18:47:38
确保您有the bundle enabled并进行了相应的配置:
在示例中:
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 MercureBundle或debug:container | grep mercure。
发布于 2021-06-18 16:55:56
我建议您不要使用PublisherInterface,而是使用HubInterface,因为PublisherInterface类现在已弃用。
下面是一个示例:
public function myFunction(HubInterface $hub){
$update = new Update("mytopic/23", ["message" => "myMessage"]);
$hub->publish($update);
return $this->json(["message" => "ok"]);
}https://stackoverflow.com/questions/66065077
复制相似问题