title: Symfony2: Backward Uri (Referrer) during switching locale
body: <p>I'd like to implement locale switcher, but it seems with no luck...</p>
  
  <p>The code below doesn't work because the (Referrer) contains the old value of locale...</p>
  
  <p>How can I redirect to the old Referrer URI with a new value of locale?</p>
  
  <p>-- routing.yml</p>
  
  <pre><code>hello:
    pattern:  /{_locale}/hello/{name}
    defaults: { _controller: JetInformBundle:Default:index, name: 'alexander' }
    requirements:
      _locale: ^en|de|ru|uk$
  
  about:
    pattern:  /{_locale}/about
    defaults: { _controller: JetInformBundle:Default:about }
    requirements:
      _locale: ^en|de|ru|uk$
  
  locale:
    pattern:  /locale/{locale}
    defaults: { _controller: JetInformBundle:Locale:index }
  </code></pre>
  
  <p>-- DefaultController.php</p>
  
  <pre><code>&lt;?php
  
  namespace Jet\InformBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use Symfony\Component\HttpFoundation\Request;
  
  class DefaultController extends Controller
  {
      public function indexAction($name, Request $request)
      {
          $request-&gt;getSession()-&gt;set('referrer', $request-&gt;getRequestUri());
          return $this-&gt;render('JetInformBundle:Default:index.html.twig',
                               array('name' =&gt; $name));
      }
  
      public function aboutAction(Request $request)
      {
          $request-&gt;getSession()-&gt;set('referrer', $request-&gt;getRequestUri());
          return $this-&gt;render('JetInformBundle:Default:about.html.twig'));
      }
  }
  </code></pre>
  
  <p>-- LocaleController.php</p>
  
  <pre><code>&lt;?php
  
  namespace Jet\InformBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use Symfony\Component\HttpFoundation\Request;
  
  class LocaleController extends Controller
  {
      public function indexAction($locale, Request $request)
      {
          $session = $request-&gt;getSession();
  
          if ($request-&gt;hasSession())
              $session-&gt;setLocale($locale);
  
          return $this-&gt;redirect($session-&gt;get('referrer'));
      }
  }
  </code></pre>
  
  <p>-- index.html.twig</p>
  
  <pre><code>{% extends '::base.html.twig' %}
  
  {% block body %}
  &lt;h1&gt;{% trans %}hello.name{% endtrans %} {{ name }}!&lt;/h1&gt;
  &lt;h3&gt;{% trans %}your.locale{% endtrans %} [{{ app.request.get('_locale') }}]&lt;/h3&gt;
  
  {% include 'JetInformBundle:Default:locales.html.twig' %}
  
  &lt;div&gt;
      &lt;p&gt;{% trans%}return.to{% endtrans%} &lt;a href="{{ path('about', { '_locale':   app.request.get('_locale') }) }}"&gt;About&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  {% endblock %}
  
  -- locales.html.twig
  
  &lt;div class="langs"&gt;
      &lt;ul&gt;
          &lt;li&gt;
              {% if app.request.get('_locale') == 'ru' %}
                  Русский
              {% else %}
                  &lt;a href="{{ path('locale', { 'locale': 'ru' }) }}"&gt;Русский&lt;/a&gt;
              {% endif %}
          &lt;/li&gt;
          &lt;li&gt;
              {% if app.request.get('_locale') == 'en' %}
                  English
              {% else %}
                  &lt;a href="{{ path('locale', { 'locale': 'en' }) }}"&gt;English&lt;/a&gt;
              {% endif %}
          &lt;/li&gt;
          &lt;li&gt;
              {% if app.request.get('_locale') == 'uk' %}
                  Украiнська
              {% else %}
                  &lt;a href="{{ path('locale', { 'locale': 'uk' }) }}"&gt;Украiнська&lt;/a&gt;
              {% endif %}
          &lt;/li&gt;
          &lt;li&gt;
              {% if app.request.get('_locale') == 'de' %}
                  Deutsch
              {% else %}
                  &lt;a href="{{ path('locale', { 'locale': 'de' }) }}"&gt;Deutsch&lt;/a&gt;
              {% endif %}
          &lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>