我想将给定请求网址的查询参数自动复制到我的路由生成的所有网址。
让我们说有人要求example.com/en?preview=true
。所以我希望在此页面上生成的所有网址都自动附加preview=true
查询参数,即不更新我的所有路由定义。
我尝试将预览参数添加为onKernelRequest侦听器中所有现有路由的默认值,但是没有走得太远。
提前致谢
答案 0 :(得分:3)
我通过扩展默认框架路由器来解决这个问题。
# src/Your/Bundle/Resources/config/service.yml
parameters:
router.class: Your\Bundle\Routing\PreviewRouter
<?php
// src/Your/Bundle/Routing/PreviewRouter.php
namespace Your\Bundle\Routing;
use Symfony\Bundle\FrameworkBundle\Routing\Router as BaseRouter;
/**
* extends the base router's generate function to always append
* the preview query param on all generated urls
*/
class PreviewRouter extends BaseRouter
{
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
// parent router generates url
$url = parent::generate($name, $parameters, $referenceType);
// check for existing preview query string
parse_str($this->getContext()->getQueryString(), $contextQueryParams);
if(isset($contextQueryParams['preview']) && filter_var($contextQueryParams['preview'], FILTER_VALIDATE_BOOLEAN))
{
// put possible query string params into $queryParams array
$urlParts = parse_url($url);
parse_str(isset($urlParts['query']) ? $urlParts['query'] : '', $urlQueryParams);
// strip everything after '?' from generated url
$url = preg_replace('/\?.*$/', '', $url);
// append merged query string to generated url
$url .= '?'.http_build_query(array_merge(
array('preview' => $contextQueryParams['preview']),
$urlQueryParams
));
}
// remove preview param if set to false deliberately
$url = preg_replace('/(\?|&)preview=(false|0|off)/', '', $url);
return $url;
}
}
答案 1 :(得分:1)
我有一个可能的实现,但有一些警告。如果您生成的网址已经包含查询字符串,则无法使用。
您需要创建一个新的Twig Filter Extension。让我们从创建扩展类开始。您可能希望从极致演示中移动并更改此设置。
//src/Acme/DemoBundle/Twig/AcmeExtension.php
<?php
namespace Acme\DemoBundle\Twig;
class AcmeExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
new \Twig_SimpleFilter('queryString', array($this, 'queryStringFilter')),
);
}
public function queryStringFilter($array)
{
return http_build_query($array);
}
public function getName()
{
return 'acme_extension';
}
}
然后您需要将此新扩展名注册为服务:
//src/Acme/DemoBundle/Resources/config/services.yml
parameters:
acme_demo.acme_extension.class: Acme\DemoBundle\Twig\AcmeExtension
services:
acme_demo.acme_extension:
class: %acme_demo.acme_extension.class%
tags:
- { name: twig.extension }
然后,您需要在主配置中包含此新服务:
//app/config/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: "@AcmeDemoBundle/Resources/config/services.yml" }
然后,当您在枝条文件中生成路线时:
<a href="{{path('some_route_id')}}?{{app.request.query.all|queryString}}">This will have your query string appended.</a>
答案 2 :(得分:1)
您可以将所有现有查询作为第二个参数添加到路径调用中:
<a href="{{ path('my_route', app.request.query.all)}}">My Link</a>