1

I'm having trouble creating a Regex to match URL slugs [a-z] ("words" separated by single dashes) like this:

wordone-wordtwo-wordtree

in route:

csa_platform_category:
    path:     /c/{slug}
    defaults: { _controller: CSAPlatformBundle:Category:index }
    requirements:
        slug: "^[a-z]+(?:-[a-z]+)*$"

in twig:

<a href="{{ path('csa_platform_category', {'slug': cat.slug}) }}">{{ cat.name }}</a>

The displayed error is:

An exception has been thrown during the rendering of a template ("Parameter "slug" for route "csa_platform_category" must match "[a-z]+(?:-[a-z]+)*" ("" given) to generate a corresponding URL.").

I'm quite bad with regular expressions, so any help would be appreciated.

yceruto
  • 9,230
  • 5
  • 38
  • 65
emwww
  • 93
  • 11
  • In PHP, you need to use regex delimiters to define regex, try `"~^[a-z]+(?:-[a-z]+)*$~"` – Wiktor Stribiżew Dec 29 '16 at 12:11
  • by adding delimiters an auther error displayed: No route found for "GET /category/mutuelle-sante"
    csa_platform_category: path: /category/{slug} defaults: { _controller: CSAPlatformBundle:Category:index } requirements: slug: "~^[a-z]+(?:-[a-z]+)*$~"
    – emwww Dec 29 '16 at 13:37
  • Ok, then the issue is just not with the regex at all. – Wiktor Stribiżew Dec 29 '16 at 13:38

1 Answers1

2

The error you are having is saying than your cat.slug is empty.

("" given)

You should check the data in cat object using var_dump or dump

goto
  • 7,908
  • 10
  • 48
  • 58