2
votes

Two month ago I asked, how to realize the routing with language parameter in uri here: Locale in Routing, Default Language also without Parameter

But now, to avoid double content, I want to have the default language (en) without and other languages with parameter. In other words i need to define the route-prefix according to the _locale parameter.

Example

http://example.com/
http://example.com/de/
http://example.com/about/
http://example.com/de/about/
...

Here is my testing setup, which gives me the error show below:

routing.yml:

_site_en:
    resource: "@MyMainBundle/Controller/SiteController.php"
    type:     annotation
    requirements: { _locale : en }
    prefix: /

_site:
    resource: "@MyMainBundle/Controller/SiteController.php"
    type:     annotation
    requirements: { _locale : fr|es|it|de }
    prefix: /{_locale}/

Controller Annotations

 /**
  * @Route( "/", name="_site_startpage", defaults={"_locale" = "", "slug"="startpage"} )
  * @Route("/{slug}/", name="_site_site", defaults={"_locale"=""})
  *
  * @ParamConverter("page", class="MyMainBundle:Page")
  */
public function siteAction( $slug, $_locale, Page $page )

How to do it the right way?

Edit: As a Result I get an Exception, that the Route to the path is not defined.

No route found for "GET /"
404 Not Found - NotFoundHttpException
1 linked Exception:
ResourceNotFoundException »

router:debug only shows the 2nd route:

_site_startpage                   ANY      /{_locale}/
_site_site                        ANY      /{_locale}/{slug}/

Edit 2: I ended up using the JMSI18nRoutingBundle…

1
"did not work" is vague. I think there already is a default locale defined in your parameters.ini, isn't there? So maybe you should try without the defaults and requirements sections.greg0ire
Sorry, you are right, i updated the question.madc
Maybe your default locale is not en does not fullfill the requirements. Try removing them to see it you still get a 404. Also run the router:debug task to see if your route is taken into account.greg0ire
I just updated the question again and reduced the example to the minimum. It looks like the second route overwrites the first one. Also the requirements defined inside the routing.yml are not working. Is there another way to define the route-prefix according to the _locale parameter? Any ideas?madc

1 Answers

2
votes

In your routing.yml for the first, the type "annotation" specifies that your controller must be introspected for routes. Everything else is ignored I think. You should try removing the type or changing it to yml, removing the resource entry and specifying the value of the _controller parameter... I think you should look at what the format for yml route definition actually is.

Or define this default route with an annotation too.