3

What could be the reason for this error in Symfony 5

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

I have this in twig:

{{ 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,
        ]);
    }
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
  • 1
    the error essentially says: I don't know how to get a Post to call that controller function. Have a look at the `ParamConverter` annotation which will help a lot here. – Jakumi Sep 13 '20 at 08:02
  • Contrary to what some of the other comments and the answer suggests, what you have should work. You of course need to have a Post entity defined and properly mapped. And you need the proper bundles installed. Etc. So look elsewhere for the problem and follow the examples in the docs. – Cerad Sep 13 '20 at 13:38
  • Just as a gross check, run "bin/console debug:event-dispatcher kernel.controller" and verify you have ParamConverterListener::onKernelController() – Cerad Sep 13 '20 at 15:11
  • @Cerad I got this [WARNING] The event "kernel.controller" does not have any registered listeners. – Emeka Mbah Sep 13 '20 at 21:03
  • I'm coming from 5 years Laravel experience. In Laravel binding nothing needs to be registered – Emeka Mbah Sep 13 '20 at 21:04
  • Laravel is not Symfony. The fact that you have absolutely nothing registered makes me think you are only using the skeleton and not the full Symfony framework. At the very least you need to SensioFrameworkExtraBundle to make this kind of conversion to work. Might want to take a glance at the docs. – Cerad Sep 13 '20 at 21:58
  • @Cerad you are right. I'm using skeleton not website-skeleton – Emeka Mbah Sep 13 '20 at 22:08

2 Answers2

3

In Symfony 5 app I fixed this issue by installing SensioFrameworkExtraBundle

composer require sensio/framework-extra-bundle
yesnik
  • 4,085
  • 2
  • 30
  • 25
2

The issue was resolved by setting router annotations in sensio_framework_extra.yaml to true. It was initially set to false, hence the error.

Thanks to @Cerad for the clue. We learn every day.

sensio_framework_extra:
    router:
        annotations: true
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96