如何解决 Symfony 5 控制器方法错误 - 无法自动装配参数

How to resolve Symfony 5 Controller method error - Cannot autowire argument

Symfony 5 中出现此错误的原因可能是什么

Cannot autowire argument $post of "App\Controller\PostController::show()": it references class "App\Entity\Post" but no such service exists.

我在树枝上有这个:

{{ path('post.show', {id:post.id}) }}
 /**
     * @Route("/show/{id}", name="show")
     * @param Post $post
     * @return Response
     */
    public function show(Post $post)
    {
        dump($post); die;
        return $this->render('post/show.html.twig', [
            'post' => $post,
        ]);
    }

通过将 sensio_framework_extra.yaml 中的 router annotations 设置为 true 解决了这个问题。 它最初设置为 false,因此出现错误。

感谢@Cerad 提供的线索。我们每天都在学习。

sensio_framework_extra:
    router:
        annotations: true

在 Symfony 5 应用程序中,我通过安装 SensioFrameworkExtraBundle

解决了这个问题
composer require sensio/framework-extra-bundle