5

I am trying with no luck to find a "referrer" object for use in my controller. I expected there would be an object similar to the request object with parameters specifying the _controller, _route and arguments.

What I am trying to do is a language switcher action that redirects the user to the same page in the new language. Something along the lines of:

public function switchLangAction($_locale)
{
    $args = array();
    $newLang = ($_locale == 'en') ? 'fr' : 'en';

    // this is how I would have hoped to get a reference to the referrer request.
    $referrer = $this->get('referrer');
    $referrerRoute = $referrer->parameters->get('_route');
    $args = $referrer->parameters->get('args'); // not sure how to get the route args out of the params either!
    $args['_locale'] = $newLang;

    $response = new RedirectResponse( $this->generateUrl(
        $referrerRoute,
        $args
    ));

    return $response;
}

It's also possible that there is another way to do this - I know in rails there is the "redirect_to :back" method for example.

Any help would be greatly appreciated.

guanchor
  • 97
  • 13
Alexander Vasilenko
  • 706
  • 1
  • 11
  • 24

2 Answers2

4

Why not changing the locale in the user's session?

First, define your locales in the router

my_login_route:
    pattern: /lang/{_locale}
    defaults: { _controller: AcmeDemoBundle:Locale:changeLang }
    requirements:
        _locale: ^en|fr$

Then, set the session

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class LocaleController extends Controller
{
    public function switchLangAction($_locale, Request $request)
    {
        $session = $request->getSession();
        $session->setLocale($_locale);
        // ... some other possible actions

        return $this->redirect($session->get('referrer'));
    }
}

In all other controllers you should set the session variable yourself by calling

$session->set('referrer', $request->getRequestUri());

You could also probably make an event listener to set the session variable for every page automatically.

kgilden
  • 10,336
  • 3
  • 50
  • 48
  • I've learned some more about Symfony2 after writing this post. A better implementation can be found [here](http://stackoverflow.com/questions/7414243/symfony2-help-please-with-backward-uri-referrer-during-switching-locale/7423276#7423276). – kgilden Sep 14 '11 at 21:37
0

It's my controller

class LocaleController extends Controller {

public function indexAction()
{
    if(null === $this->getRequest()->getLocale()){
        $locale = $this->getRequest()->getPreferredLanguage($this->getLocales());
        $this->getRequest()->setLocale($locale);
    }
    else{
        $locale = $this->getRequest()->getLocale();
    }

    return $this->redirect($this->generateUrl('corebundle_main_index', array('_locale' => $locale)));
}

public function changeLocaleAction($_locale)
{
    $request = $this->getRequest();
    $referer = $request->headers->get('referer');
    $locales = implode('|',$this->getLocales());
    $url = preg_replace('/\/('.$locales.')\//', '/'.$_locale.'/', $referer, 1);
    return $this->redirect($url);
}

private function getLocales()
{
    return array('ru', 'uk', 'en');
}


/**
 * @Template()
 */
public function changeLocaleTemplateAction()
{
    return array();
}

}

rtyshyk
  • 942
  • 1
  • 15
  • 29