Symfony entity parameter converter doesn't get route items correctly
P粉885562567
P粉885562567 2023-08-28 22:55:25
0
1
434
<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粉885562567
P粉885562567

reply all(1)
P粉129731808

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
{
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!