Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

How to have common parameters for every route

Let's take an example. You want to have the user culture in front of each of your URL.

So, your routing configuration look like something like that:

page:
  url: /:culture/:page
  params: ...

article:
  url: /:culture/:year/:month/:day/:slug
  params: ...

...

This is the easy part. But now, every time you call url_for() or link_to() you have to pass the culture parameter.

For such situations, there is a better way. The routing has a special configuration parameter named sf_routing_defaults that you can set with default values:

sfConfig::set('sf_routing_defaults', array('culture' => 'en'));

And now, the culture parameter will automatically added to the parameters that you pass to the url_for() or link_to() helpers.

by Fabien Potencier on 2006-08-23, tagged i18n  routing 

Comments on this snippet

gravatar icon
#1 halfer on 2006-08-24 at 04:11

Great snippet, thanks Fabien. This will prove very useful.

gravatar icon
#2 Pierre Minnieur on 2006-08-30 at 04:21

Where do I have to put this one? In my config.php? Or Can I add it in one of my .yml-files in the config folder?

gravatar icon
#3 Sylvain PAPET on 2006-10-18 at 02:49

myCultureFilter filter

myapp/config/filters.yml

cultureFilter:
  class: myCultureFilter

myapp/lib/myCultureFilter.class.php

class myCultureFilter extends sfFilter
{
 
  public function execute ($filterChain)
  {
    $this->getContext()->getUser()->setCulture($this->getContext()->getRequest()->getParameter('culture', 'fr'));
    sfConfig::set('sf_routing_defaults', array('culture' => $this->getContext()->getRequest()->getParameter('culture', 'fr')));
    $filterChain->execute();
  }
 
}
gravatar icon
#4 Francois Zaninotto on 2006-11-16 at 07:02

Since r2663, you can directly use the :sf_culture variable in your routes; it is automatically set to the user culture, and it removes the need for a filter.

page:
  url: /:sf_culture/:page
  params: ...
gravatar icon
#5 Francois Zaninotto on 2006-11-16 at 07:13

For the language-dependent routing to work inbound, you have to add a filter anyway. It will check if the sf_culture request parameter is different from the current user culture, and if so, change the user culture. Something like:

class switchLanguageFilter extends sfFilter
{ 
  public function execute ($filterChain)
  {
    $context = $this->getContext();
    $user = $context->getUser();
    $url_culture = $context->getRequest()->getParameter('sf_culture', sfConfig::get('sf_i18n_default_culture'));
    if($url_culture != $user->getCulture())
    {
      // The user wants to see the page in another language
      $user->setCulture($url_culture);
    }
    $filterChain->execute();
  } 
}
gravatar icon
#6 Krešo Kunjas on 2006-11-22 at 01:58

does this (except for comments my francois Z.) works on current stable ( 0.6.3 ) coz i cannot get it to work, the culture doesn't show in generated urls

gravatar icon
#7 Roberto G Puentes Diaz on 2008-02-20 at 12:13

Hi Fabien!

I have a challenger, that i cann't solved. Is about URLs (routing) and I18N. I need have URLs for each culture: example : site.com/goodday site.com/buendia site.com/bongiorno

I think that, site.com/en/goodday site.com/es/goodday site.com/it/goodday

is a partial solution, because, someone with culture spanish o italian, do not know what means "goodday"

Someone in the Forum, post: regla: url_es: /hola url_en: /hello params: { .... }

Other say: have 2 URL are a same routing rule "@home" and point to same module/action. I think a solution is have a routing.yml (default), or routing.en.yml and routing.es.yml and routing.it.yml

I'am relative new in symfony, i build my first project, and this is a really nice feature for Symfony.

Cheers

Roberto (puentesdiaz@gmail.com)

You need to create an account or log in to post a comment or rate this snippet.