Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 651
JohnDoe66 Avatar asked Aug 07 '13 08:08

JohnDoe66


People also ask

How does Symfony handle routing requests?

Every request sent to a symfony application is first analyzed by the routing system (which is simple because every request is handled by a single front controller). The routing system looks for a match between the request URL and the patterns defined in the routing rules.

How to use a URL in a Symfony application?

The application can format a URL to bring information to the user, and the user can use the URL to access resources of the application. This is possible in symfony applications, because the URL presented to the end user is unrelated to the server instruction needed to perform the request.

What is the syntax for internal URIs in Symfony?

To make things easy, symfony uses a syntax for internal URIs very similar to the one of regular URLs. Listing 9-1 shows an example. // Internal URI syntax <module>/<action> [?param1=value1] [&param2=value2] [&param3=value3]...

How does Symfony test my_rule?

For instance, the URL /foo/123 matches both of the rules defined in Listing 9-16, but symfony first tests my_rule:, and as that rule matches, it doesn't even test the default: one. The request is handled by the mymodule/myaction action with bar set to 123 (and not by the foo/123 action).


1 Answers

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:     .+
like image 152
sf_tristanb Avatar answered Sep 29 '22 13:09

sf_tristanb