Search code examples
phpsymfonyroutessymfony4

Symfony4 route param order


I have route with 1 slug, 2 int (category, page):

@Route("/articles-{categorySlug}-{category}-{page}", name="article.list.category", defaults={"page": 1}, requirements={"category": "\d+", "page": "\d+", "categorySlug": "[0-9a-zA-Z\/\-]*"})

Good work:

Url: /articles-categorySlug-5
Result: categorySlug: categorySlug, category: 5, page: 1 (default)

Bad work (to fix):

Url: /articles-categorySlug-5-2
Result: categorySlug: categorySlug, category: 2 (should be 5), page: 1 (should be 2)

How I can fix that?


Solution

  • This is because the category slug regex is greedy. Add a trailing ? to make it ungreedy:

    @Route("/articles-{categorySlug}-{category}-{page}", name="article.list.category", defaults={"page": 1}, requirements={"category": "\d+", "page": "\d+", "categorySlug": "[0-9a-zA-Z\/\-]*?"})