Symfony实体参数转换器未获取正确的路线项目

c0vxltue  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(136)

我有一个路线,需要抓住一个类别,然后一个子类别的路线看起来像这样:

#[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

我尝试转到/mtg/dmr,但@ParamConverter没有找到404对象。当我在分析器中查看Doctrine日志时,系统正在查找正确的表,但对于这两个表,都在查找mtg,而不是首先查找mtg,然后查找dmr。您知道这是怎么回事吗?

6bc51xsx

6bc51xsx1#

DOC示例:

#[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)
{
}

"所以,在你的情况下,你必须有:"

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

qfe3c7zg2#

方法中的参数名必须与paramConverter中的参数名相同,
文件:
https://symfony.com/bundles/SensioFrameworkExtraBundle/current/annotations/converters.html
apply()方法在配置被支持时被调用,根据请求属性,它应该设置一个名为$configuration-〉getName()的属性,该属性存储一个类为$configuration-〉getClass()的对象。
按照您的代码,对象在$request-〉属性上设置为 * category!= $cat(您的参数),subCategory!= $sub,您将使用null而不是对象

名称必须相同

#[Route('/{slug}/{subSlug}', name: 'subcategory')]
#[Entity('category', expr: 'repository.findOneBySlug(slug)')]
#[Entity('subcategory', expr: 'repository.findOneBySlug(subSlug)')]
public function subcat(Category $category, Subcategory $subcategory): Response

相关问题