Symfony2:Referrer 对象类似于 Request 对象?
我试图找到一个“推荐人”对象,但没有运气在我的中使用 控制器。我预计会有一个与请求类似的对象 带有指定 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不在用户会话中更改区域设置?
首先,在路由器中定义您的区域设置
然后,设置会话
在所有其他控制器中,您应该通过调用自己设置会话变量
您也可以创建一个事件侦听器来自动为每个页面设置会话变量。
Why not changing the locale in the user's session?
First, define your locales in the router
Then, set the session
In all other controllers you should set the session variable yourself by calling
You could also probably make an event listener to set the session variable for every page automatically.
这是我的控制器
It's my controller