Home  >  Q&A  >  body text

Symfony entity parameter converter doesn't get route items correctly

<p>I have a route that needs to get categories and subcategories. The route is in the following form: </p> <pre class="brush:php;toolbar:false;">#[Route('/{slug}/{subSlug}', name: 'subcategory')] #[Entity('category', expr: 'repository.findOneBySlug(slug)')] #[Entity('subcategory', expr: 'repository.findOneBySlug(subSlug)')] public function subcat(Category $cat, Subcategory $sub): Response</pre> <p>I tried to access <code>/mtg/dmr</code>, but I got a 404 Object not found error, caused by @ParamConverter. When I look in the Doctrine log, the system is looking in the correct table, but for both it is looking for <code>mtg</code> instead of first looking for <code>mtg</code> and then < code>dmr</code>. Any ideas what's going on? </p>
P粉885562567P粉885562567390 days ago391

reply all(1)I'll reply

  • P粉129731808

    P粉1297318082023-08-29 19:01:43

    DOC example:

    #[Route('/blog/{date}/{slug}/comments/{comment_slug}')]
    #[ParamConverter('post', options: ['mapping' => ['date' => 'date', 'slug' => 'slug']])]
    #[ParamConverter('comment', options: ['mapping' => ['comment_slug' => 'slug']])]
    public function showComment(Post $post, Comment $comment)
    {
    }

    So, in your case you must have:

    #[Route('/{slug}/{subSlug}', name: 'subcategory')]
    #[ParamConverter('cat', options: ['mapping' => ['slug' => 'slug']])]
    #[ParamConverter('sub', options: ['mapping' => ['subSlug' => 'slug']])]
    public function (Category $cat, Subcategory $sub): Response
    {
    }

    reply
    0
  • Cancelreply