This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Custom URL Matcher

Discuss installing Symfony 2 here

Custom URL Matcher

by deltorro » Sun Feb 23, 2014 6:39 pm

For my project I want to have urls with an optional locale. I know apple.com offers this construction as well.
So for example: the url of the page about the iMac is /imac. The url of the French version of this page however is /fr/imac. It means the locale in the url is optional and defaults to 'en'.

1. First, in order to make this work, I thought it would be best to create my own UrlMatcher class. Am I right?

In that case, I guess I should add the name of this class in 'config.yml' to 'framework.router.matcher_class'. However, whichever value I add here, it always fails. Also if I add the default value of 'Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher' here. It fails because I get an error saying 'Unrecognized options "matcher_class" under "framework.router"'.

2. In what way do I need to add the classname of my custom UrlMatcher to config.yml?

Anyone who can help me out with this one?
deltorro
Junior Member
 
Posts: 3
Joined: Sun Feb 23, 2014 6:27 pm

Re: Custom URL Matcher

by nymo » Tue Feb 25, 2014 1:17 pm

nymo
Senior Member
 
Posts: 174
Joined: Fri Jan 07, 2011 11:37 pm
Location: Germany

Re: Custom URL Matcher

by deltorro » Fri Feb 28, 2014 12:23 pm

nymo, I'd already read that part of the documentation. But like you can read in my topic start I want an optional locale in the url.

With the pattern described in the documentation it won't be possible with a single route to make the locale 'en' for /imac but 'fr' for /fr/imac.

How can I solve this?
deltorro
Junior Member
 
Posts: 3
Joined: Sun Feb 23, 2014 6:27 pm

Re: Custom URL Matcher

by nymo » Mon Mar 03, 2014 10:34 am

Ok I understand, have you read this part?

http://symfony.com/doc/current/book/routing.html#adding-requirements

Code: Select all
homepage:
    path:      /{culture}
    defaults:  { _controller: AcmeDemoBundle:Main:homepage, culture: en }
    requirements:
        culture:  en|fr


This will result in the following:

    / {culture} = en
    /en {culture} = en
    /fr {culture} = fr
    /es won't match this route

So I think your solution would result in:
    /en/imac
    /fr/imac
via one route and if you want to use something like
    /imac
    /fr/imac
you will have to use two routes.

I would create just two routes and use an easy way so for example:
Route 1:
Code: Select all
english-way:
    path:      /{slug}
    defaults:  { _controller: AcmeDemoBundle:Main:Article }
   


Route 2:
Code: Select all
foreign-way:
    path:      /{culture}/{slug}
    defaults:  { _controller: AcmeDemoBundle:Main:ForeignArticle }
    requirements:
        culture:  es|fr|...


How do you know that apple.com uses for their implementation just one route?
nymo
Senior Member
 
Posts: 174
Joined: Fri Jan 07, 2011 11:37 pm
Location: Germany

Re: Custom URL Matcher

by deltorro » Tue Mar 04, 2014 12:28 pm

Thanks nymo, I already thought it was hard to do it with one route.

Do you know the answer to my second question in the topic start? "In what way do I need to add the classname of my custom UrlMatcher to config.yml". Because adding the default value of 'Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher' fails because of an error saying 'Unrecognized options "matcher_class" under "framework.router"'. What am I doing wrong here?
deltorro
Junior Member
 
Posts: 3
Joined: Sun Feb 23, 2014 6:27 pm

Re: Custom URL Matcher

by nymo » Wed Mar 05, 2014 2:36 pm

Regards your second question currently I'm not sure how to achieve this. Via config file it's not possible as you can see in the doc there is no matcher_class key you can use. http://symfony.com/doc/current/reference/configuration/framework.html

Looks like you have to do it the manually way and replace the matcher in someway. I found only this entry in the docs: http://symfony.com/doc/current/components/routing/introduction.html not sure if this is of any help sorry.
nymo
Senior Member
 
Posts: 174
Joined: Fri Jan 07, 2011 11:37 pm
Location: Germany