Php Symfony 2指向当前页面的链接
如何在我的模板中创建指向当前模板的链接 我想创建一个语言切换器,该切换器应以varios语言链接到当前页面,因此所有参数应与区域设置相同。Php Symfony 2指向当前页面的链接,php,symfony,twig,routes,Php,Symfony,Twig,Routes,如何在我的模板中创建指向当前模板的链接 我想创建一个语言切换器,该切换器应以varios语言链接到当前页面,因此所有参数应与区域设置相同。 <a href="{{ path(app.request.attributes.get('_route')) }}">Current page</a> 类似问题:我最终为此使用了自己的函数。起初我以为它包含在FrameworkBundle的某个地方,但没有找到任何关于它的信息。这里是我采取的步骤 首先,我创建了一个细枝扩展函数,该
<a href="{{ path(app.request.attributes.get('_route')) }}">Current page</a>
类似问题:我最终为此使用了自己的函数。起初我以为它包含在
FrameworkBundle
的某个地方,但没有找到任何关于它的信息。这里是我采取的步骤
首先,我创建了一个细枝扩展函数,该函数将输出与用户当前访问的路径相同的路径(包括参数和查询字符串)。我把这一步留给你了。如果您还不知道如何创建细枝扩展,您可以从Symfony2的良好教程中了解如何创建细枝扩展。如果你需要的话,我可以帮你
下一步是创建将切换当前路由的区域设置的函数本身。此函数需要请求
和路由器
对象作为依赖项。就我个人而言,我将此功能放在一个名为routinghelp
service的专用服务中。然后,我的小枝扩展功能将使用此服务。这里是我添加到依赖项容器中的服务定义:
acme.helper.routing:
class: Application\AcmeBundle\Helper\RoutingHelper
scope: "request"
arguments:
request: "@request"
router: "@router"
以及我的服务的构造器:
protected $request;
protected $router;
public function __construct($request, $router)
{
$this->request = $request;
$this->router = $router;
}
$locale参数是要切换到的新语言环境。以下是功能:
public function localizeCurrentRoute($locale)
{
$attributes = $this->request->attributes->all();
$query = $this->request->query->all();
$route = $attributes['_route'];
# This will add query parameters to attributes and filter out attributes starting with _
$attributes = array_merge($query, $this->filterPrivateKeys($attributes));
$attributes['_locale'] = $locale;
return $this->router->generate($route, $attributes);
}
本质上,它做了其他人到目前为止所做的事情,但它也处理参数和查询字符串。方法filterPrivateKeys
将从路由属性中删除私有属性。这些属性以下划线开头,不应传递回路由生成器。这里是它的定义:
private function filterPrivateKeys($attributes)
{
$filteredAttributes = array();
foreach ($attributes as $key => $value) {
if (!empty($key) && $key[0] != '_') {
$filteredAttributes[$key] = $value;
}
}
return $filteredAttributes;
}
最后,我可以在我的Twig视图中创建链接以切换区域设置:
{% block language_bar %}
<a href="{{ localize_route('en') }}"> English </a>
<a href="{{ localize_route('fr') }}"> Français </a>
{% endblock %}
在twig扩展函数中,我有一个调用:$routinghelp=$this->container->get('acme.helper.routing')代码>
这应该可以解决范围扩大异常,因为细枝扩展不在请求范围内
更新:
Symfony 2.1现在可以比以前更轻松地使用区域设置切换器。事实上,Symfony的2.1版本为routes引入了一个新参数,使得进行区域设置切换更加容易。这里的代码,都在细枝
{% set route_params = app.request.attributes.get('_route_params') %}
{# merge the query string params if you want to keep them when switching the locale #}
{% set route_params = route_params|merge(app.request.query.all) %}
{# merge the additional params you want to change #}
{% set route_params = route_params|merge({'_locale': 'fr'}) %}
{{ path(app.request.attributes.get('_route'), route_params) }}
它仍然只有几行twig代码,但可以包含在twig块中以便于重用。来自Symfony社区的stof获得上述代码的学分
希望这就是你要找的
问候,
Matt是的,但我还想使用当前参数,只更改区域设置。是的,这是我已经找到的,我还需要当前参数为带有参数的页面构建路由。好的,所以我必须自己编写。我认为在这个框架中一定有类似的东西,因为它是一个基本函数。我成功地编写了插件和助手类,但是现在我的作用域出现了问题。由于帮助程序和细枝扩展位于:“作用域:”请求“”中,而细枝本身不在其中(检测到作用域加宽注入:定义“细枝”引用属于较窄作用域的服务“s2_tools.twig.routing_helper”)。你是怎么做到的?是的,我也想到了,但还没发现什么。我想看看内置的东西。我确实通过注入完整服务容器而不是在其中检索我的助手来解决这个问题。我编辑了我的答案以反映这一点。好的,我现在注入了完整的服务容器。它现在起作用了。谢谢。@Parhs如果你有一个更简单的解决方案,我想在我做这件事的时候听听,我觉得这是一个很大的工作。也许这个解决方案可以集成到类似的RouterBundle中,以便于重用…@Parhs在我的答案中添加了一个更新,使用较少的工作技术在Symfony 2.1中实现相同的功能。
acme.twig.extension:
class: Application\AcmeBundle\Twig\Extension\AcmeExtension
arguments:
container: "@service_container"
tags:
- { name: twig.extension }
{% set route_params = app.request.attributes.get('_route_params') %}
{# merge the query string params if you want to keep them when switching the locale #}
{% set route_params = route_params|merge(app.request.query.all) %}
{# merge the additional params you want to change #}
{% set route_params = route_params|merge({'_locale': 'fr'}) %}
{{ path(app.request.attributes.get('_route'), route_params) }}