-2

I updated symfony from 4.4 to 5.0 and get errors like this

Cannot autowire argument $category of "App\Controller\CategoryController::category()": it references class "App\Entity\Category" but no such service exists.

My Controller code below.

/**
     * Renders category page with subcategories
     * @param Category $category
     * @return mixed
     */   
use App\Entity\Category;

public function category(Category $category) {
return $this->render('category/index.html.twig', [
    'category' => $category
]);

This code works on symfony 4.4. How can i fix it?

Kris
  • 121
  • 1
  • 10
  • 2
    You don't really autowire entities per se. Instead, you use a param converter which is smart enough to extract the category's id from the request and automatically load the category entity. Are you using annotations for your routes? If so, consider updating your question with the annotation because that is where the param conversion is configured. https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html – Cerad Jun 22 '20 at 17:13
  • I don't use annotation for routes. – Kris Jun 22 '20 at 17:25
  • So check the docs and see where your param converter is being wired up. – Cerad Jun 22 '20 at 17:38
  • Try "bin/console debug:event-dispatcher | grep Extra" and verify you have a ParamConverterListener::onKernelController() listener running. Don't know why you would not but that is where the magic happens. – Cerad Jun 22 '20 at 18:09
  • I got "#4 Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController() 0 " – Kris Jun 22 '20 at 18:11
  • Don't know what to tell you. I just ran a quick test using a fresh 5.1 install and /shoe/show/{id} retrieved and injected the shoe just fine. Maybe use "bin/console debug:router shoe_show" to verify you have a query parameter named 'id' defined in your route. – Cerad Jun 22 '20 at 18:22
  • Yes, i have "/product/{slug}/" – Kris Jun 22 '20 at 18:40
  • Kind of a funny url for a category. The slug identifies the category? In other words, CategoryRepository::findOneBy(['slug' => $slug]) will return the expected category? I would have expected /product would involve product entities. – Cerad Jun 22 '20 at 20:03

1 Answers1

0

It was because roukmoute/hashids-bundle. After deleting this bundle all works fine.

Kris
  • 121
  • 1
  • 10