Language switcher, redirect to current page with symfony

笑着哭i 提交于 2019-12-23 18:36:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!