Symfony2:Referrer 对象类似于 Request 对象?

发布于 2024-11-29 17:23:48 字数 871 浏览 0 评论 0原文

我试图找到一个“推荐人”对象,但没有运气在我的中使用 控制器。我预计会有一个与请求类似的对象 带有指定 _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”方法。

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-12-06 17:23:48

为什么不在用户会话中更改区域设置?

首先,在路由器中定义您的区域设置

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

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

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.

十级心震 2024-12-06 17:23:48

这是我的控制器

类 LocaleController 扩展了控制器 {

公共函数indexAction()
{
    if(null === $this->getRequest()->getLocale()){
        $locale = $this->getRequest()->getPreferredLanguage($this->getLocales());
        $this->getRequest()->setLocale($locale);
    }
    别的{
        $locale = $this->getRequest()->getLocale();
    }

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

公共函数changeLocaleAction($_locale)
{
    $request = $this->getRequest();
    $referer = $request->headers->get('referer');
    $locales = implode('|',$this->getLocales());
    $url = preg_replace('/\/('.$locales.')\//', '/'.$_locale.'/', $referer, 1);
    返回 $this->redirect($url);
}

私有函数 getLocales()
{
    返回数组('ru','uk','en');
}


/**
 * @模板()
 */
公共函数changeLocaleTemplateAction()
{
    返回数组();
}

}

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文