JohnDoe66
JohnDoe66

Reputation: 875

Error in Symfony2 when URL contains dot(.)

I use Symfony 2.2.4, and I try desperately to generate URL (with Twig). In fact, I obtain always the same error when my URL cointain a dot.

For example : - Route: "my_route" - First parameter: "id" - Second parameter: "title"

In Twig:

{{ path("my_route", {"id" : 1984, "title" : "another...test"}) }}

I obtain the following error:

An exception has been thrown during the rendering of a template ("Parameter "title" for route "my_route" must match "[^/.]++" ("another...test" given) to generate a corresponding URL.") in ...

I've tried with Symfony 2.0.3, and there are no problem.

Have you got an idea to resolve this problem?

Thanks by advance for your help.

Best regards

Upvotes: 5

Views: 5008

Answers (2)

maxidirienzo
maxidirienzo

Reputation: 99

Old question, but since there still are old SF codebases out there, this may save up some time to someone.

This is an issue with how SF compiles the regex to match the routes. This applies at least to version 1.x and 2.x, not sure about newer versions.

In your routing.yml you need to specify the expected format of your title parameter under requirements, as shown below:

my_route:
  url: /my_route/:id/:title
  param: { module: your_module, action: your_action }
  requirements: {title: .+}

Alternatively, instead of using requirements, specifying the segment separator on your route also does the trick:

my_route:
  url: /my_route/:id/:title
  param: { module: your_module, action: your_action }
  options: { segment_separators: [/] }

Upvotes: 0

sf_tristanb
sf_tristanb

Reputation: 8855

If you use a suffix, you should add it in the requirement of the route and use {_format} instead of "html" :

Example from the documentation :

article_show:
  path:     /id/{title}.{_format}
  defaults: { _controller: AcmeDemoBundle:Article:show, _format: html }
  requirements:
      _format:  html
      title:     .+

EDIT :

You should avoid using dots (".") for your parameter. You should really use a slug of your title. But you could try in the requirements a regex to allow having dots in the title parameter.

  requirements:
      _format:  html
      title:     .+

Upvotes: 7

Related Questions