【问题标题】:Symfony2: Referrer object similar to Request object?Symfony2:类似于Request对象的Referrer对象?
【发布时间】:2011-08-17 06:55:46
【问题描述】:

我试图找到一个“推荐人”对象用于我的 控制器。我预计会有一个类似于请求的对象 带有参数的对象,指定 _controller、_route 和 论据。

我正在尝试做的是一个 语言切换器 重定向操作 用户转到新语言的同一页面。沿途的东西 行:

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;
}

也有可能有另一种方法来做到这一点 - 我知道 例如,rails 有“redirect_to :back”方法。

任何帮助将不胜感激。

【问题讨论】:

    标签: php localization symfony


    【解决方案1】:

    为什么不在用户会话中更改语言环境?

    首先,在路由器中定义您的语言环境

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

    然后,设置会话

    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'));
        }
    }
    

    在所有其他控制器中,您应该通过调用自己设置会话变量

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

    您还可以创建一个事件侦听器来自动为每个页面设置会话变量。

    【讨论】:

    • 写完这篇文章后,我对 Symfony2 有了更多了解。可以在here 找到更好的实现。
    【解决方案2】:

    这是我的控制器

    类 LocaleController 扩展控制器 {

    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();
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-30
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 2013-12-30
      相关资源
      最近更新 更多