【问题标题】:Symfony Mercure Error Symfony\Component\Mercure\PublisherInterfaceSymfony Mercure 错误 Symfony\Component\Mercure\PublisherInterface
【发布时间】:2021-05-09 21:38:21
【问题描述】:
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 的官方文档,看了一些教程,他们似乎没有这个问题。您知道可能存在什么问题吗?谢谢!

【问题讨论】:

  • 你对 Symfony 在错误消息中问你的问题的回答是什么:Did you create a class that implements this interface?
  • 您的控制器是否启用了自动装配 (symfony.com/doc/current/service_container/autowiring.html)?打开您的services.yml 并检查autowire 是否设置为true,您的控制器是否在./src/Controller/ 文件夹中,并且在services.yml 中您有这个:App\Controller\: resource: '../src/Controller/' tags: ['controller.service_arguments']

标签: php symfony mercure


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    这是一个例子:

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

    【讨论】:

      【解决方案3】:

      确保您拥有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 MercureBundledebug:container | grep mercure

      【讨论】:

        猜你喜欢
        • 2021-12-27
        • 2021-09-03
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-16
        • 2019-12-06
        • 2021-06-29
        相关资源
        最近更新 更多