Symfony2:切换语言环境期间向后 Uri(Referrer)
我想实现区域设置切换器,但似乎没有运气...
下面的代码不起作用,因为 (Referrer) 包含区域设置的旧值...
我如何使用 a 重定向到旧的 Referrer URI locale 的新值?
--routing.yml
hello:
pattern: /{_locale}/hello/{name}
defaults: { _controller: JetInformBundle:Default:index, name: 'alexander' }
requirements:
_locale: ^en|de|ru|uk$
about:
pattern: /{_locale}/about
defaults: { _controller: JetInformBundle:Default:about }
requirements:
_locale: ^en|de|ru|uk$
locale:
pattern: /locale/{locale}
defaults: { _controller: JetInformBundle:Locale:index }
--DefaultController.php
<?php
namespace Jet\InformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function indexAction($name, Request $request)
{
$request->getSession()->set('referrer', $request->getRequestUri());
return $this->render('JetInformBundle:Default:index.html.twig',
array('name' => $name));
}
public function aboutAction(Request $request)
{
$request->getSession()->set('referrer', $request->getRequestUri());
return $this->render('JetInformBundle:Default:about.html.twig'));
}
}
--LocaleController.php
<?php
namespace Jet\InformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
class LocaleController extends Controller
{
public function indexAction($locale, Request $request)
{
$session = $request->getSession();
if ($request->hasSession())
$session->setLocale($locale);
return $this->redirect($session->get('referrer'));
}
}
--index.html.twig
{% extends '::base.html.twig' %}
{% block body %}
<h1>{% trans %}hello.name{% endtrans %} {{ name }}!</h1>
<h3>{% trans %}your.locale{% endtrans %} [{{ app.request.get('_locale') }}]</h3>
{% include 'JetInformBundle:Default:locales.html.twig' %}
<div>
<p>{% trans%}return.to{% endtrans%} <a href="{{ path('about', { '_locale': app.request.get('_locale') }) }}">About</a></p>
</div>
{% endblock %}
-- locales.html.twig
<div class="langs">
<ul>
<li>
{% if app.request.get('_locale') == 'ru' %}
Русский
{% else %}
<a href="{{ path('locale', { 'locale': 'ru' }) }}">Русский</a>
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'en' %}
English
{% else %}
<a href="{{ path('locale', { 'locale': 'en' }) }}">English</a>
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'uk' %}
Украiнська
{% else %}
<a href="{{ path('locale', { 'locale': 'uk' }) }}">Украiнська</a>
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'de' %}
Deutsch
{% else %}
<a href="{{ path('locale', { 'locale': 'de' }) }}">Deutsch</a>
{% endif %}
</li>
</ul>
</div>
I'd like to implement locale switcher, but it seems with no luck...
The code below doesn't work because the (Referrer) contains the old value of locale...
How can I redirect to the old Referrer URI with a new value of locale?
-- routing.yml
hello:
pattern: /{_locale}/hello/{name}
defaults: { _controller: JetInformBundle:Default:index, name: 'alexander' }
requirements:
_locale: ^en|de|ru|uk$
about:
pattern: /{_locale}/about
defaults: { _controller: JetInformBundle:Default:about }
requirements:
_locale: ^en|de|ru|uk$
locale:
pattern: /locale/{locale}
defaults: { _controller: JetInformBundle:Locale:index }
-- DefaultController.php
<?php
namespace Jet\InformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function indexAction($name, Request $request)
{
$request->getSession()->set('referrer', $request->getRequestUri());
return $this->render('JetInformBundle:Default:index.html.twig',
array('name' => $name));
}
public function aboutAction(Request $request)
{
$request->getSession()->set('referrer', $request->getRequestUri());
return $this->render('JetInformBundle:Default:about.html.twig'));
}
}
-- LocaleController.php
<?php
namespace Jet\InformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
class LocaleController extends Controller
{
public function indexAction($locale, Request $request)
{
$session = $request->getSession();
if ($request->hasSession())
$session->setLocale($locale);
return $this->redirect($session->get('referrer'));
}
}
-- index.html.twig
{% extends '::base.html.twig' %}
{% block body %}
<h1>{% trans %}hello.name{% endtrans %} {{ name }}!</h1>
<h3>{% trans %}your.locale{% endtrans %} [{{ app.request.get('_locale') }}]</h3>
{% include 'JetInformBundle:Default:locales.html.twig' %}
<div>
<p>{% trans%}return.to{% endtrans%} <a href="{{ path('about', { '_locale': app.request.get('_locale') }) }}">About</a></p>
</div>
{% endblock %}
-- locales.html.twig
<div class="langs">
<ul>
<li>
{% if app.request.get('_locale') == 'ru' %}
Русский
{% else %}
<a href="{{ path('locale', { 'locale': 'ru' }) }}">Русский</a>
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'en' %}
English
{% else %}
<a href="{{ path('locale', { 'locale': 'en' }) }}">English</a>
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'uk' %}
Украiнська
{% else %}
<a href="{{ path('locale', { 'locale': 'uk' }) }}">Украiнська</a>
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'de' %}
Deutsch
{% else %}
<a href="{{ path('locale', { 'locale': 'de' }) }}">Deutsch</a>
{% endif %}
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让我向您展示我的解决方案。我编写了内核事件监听器:
如下所示:
它只是在用户每次打开页面时保存最后一个请求路由数据。在控制器中,当用户想要更改区域设置时,我会这样做:
Let me show you my solution. I've written kernel event listener:
like the following:
It just saves last request route data each time user opens a page. And in controller when user wants to change locale I do this:
我使用
gilden 建议的方法提供了我的解决方案
I provide my solution using the approach with
as suggested by gilden
您必须在会话中存储路由属性的数组,而不是单个 url。
供将来参考以及任何遇到此问题的人:您不必通过在每个控制器中设置来将
referrer
属性存储在会话中。您可以从headers
属性中检索之前的 url:有关其他信息,请参阅:
ParameterBag
类 githubYou must store an array of your route attributes in the session instead of a single url.
For future reference and to anyone stumbling over this: you don't have to store the
referrer
attribute in your session by setting it in EVERY controller. You can retrieve the previous url from theheaders
property:For additional info, consult:
Request
class on githubParameterBag
class on github吉尔登,非常感谢您的想法,但它无论如何都不起作用...
这是控制器 LOCALE SWITCHER:
这是业务逻辑的控制器:
关于 array_key_diff() 的忽略参数...
我尝试包括一些其中有新一代路线,但运气不佳......
不明白发生了什么...
这似乎只是一个 hack...我们是否有更复杂的方法来实现 Symfony2 中的这个简单功能? RAW PHP 允许我做这么多事情&更容易...
再次感谢。
Gilden, thanks a lot for idea, but it's doesn't work anyway...
Here is the controller LOCALE SWITCHER:
Here is the controller of BUSINESS LOGIC:
Regarding IGNORED params for array_key_diff()...
I've tried to include few of them for the new route generation, but with no luck...
Don't understand what is going on...
It's seems just a hack... Do we have more sofisticated way to realize this simple feature in Symfony2 ?? RAW PHP allows me to do that much & much more easily...
Thanks again.