首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony Mercure错误Symfony\Component\Mercure\PublisherInterface

Symfony Mercure错误Symfony\Component\Mercure\PublisherInterface
EN

Stack Overflow用户
提问于 2021-02-05 22:30:38
回答 2查看 298关注 0票数 0
代码语言:javascript
复制
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);
    }

我得到了这个错误:

代码语言:javascript
复制
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?
代码语言:javascript
复制
/*
 * 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的官方文档,看了一些教程,他们似乎没有这个问题。你知道问题出在哪里吗?谢谢!

EN

回答 2

Stack Overflow用户

发布于 2021-03-25 18:47:38

确保您有the bundle enabled并进行了相应的配置:

在示例中:

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2021-06-18 16:55:56

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

下面是一个示例:

代码语言:javascript
复制
public function myFunction(HubInterface $hub){

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

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

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66065077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档