搜索

首页  >  问答  >  正文

Symfony实体参数转换器无法正确获取路由项

<p>我有一个需要获取类别和子类别的路由,路由的形式如下:</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>我尝试访问 <code>/mtg/dmr</code>,但是我得到了一个404的对象未找到错误,由@ParamConverter引起。 当我在Doctrine日志中查看时,系统在正确的表中查找,但是对于两个都在寻找<code>mtg</code>而不是先找<code>mtg</code>然后找<code>dmr</code>。有什么想法是怎么回事吗?</p>
P粉885562567P粉885562567442 天前445

全部回复(1)我来回复

  • P粉129731808

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

    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
    {
    }

    回复
    0
  • 取消回复