【发布时间】: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']