问题
What is the best way to do a language switcher in symfony that redirects to the same page in the chosen language? Jobeet simply redirects on the homepage.
回答1:
Something like so should do the trick:
<?php
class myActions extends sfActions
{
public function executeLanguageSwitch(sfWebRequest $request)
{
$new_language = $request->getParameter('lang',false);
$this->forward404unless($new_language);
// You should probably insert stuff here check that the new culture passed in is valid
$this->getUser()->setCulture($new_language);
$this->redirect($request->getReferer());
return sfView::HEADER_ONLY;
}
}
回答2:
This works for me:
<?php
class PageController extends Controller
{
public function changeLocaleAction(Request $request)
{
$locale = $request->get('_locale');
$this->get('session')->set('_locale', $locale);
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
}
来源:https://stackoverflow.com/questions/4359125/language-switcher-redirect-to-current-page-with-symfony