解決方案#1只需要添加一個if從句
{% if BasePath == 'post.' %}
{{ url(BasePath~'view', {'post_id':post.postId}) }}
{% elseif BasePath == 'post.current_' %}
{{ url(BasePath~'view') }}
{% endif %}
也許不是最優雅的,但應工作。
解決方案#2
劈裂網址帶有問號和獲得的第一個字符串
{% set myUrl = url(basePath~'view', {'post_id':post.postId}) %}
{{ myUrl|split("?")|first }}
解決方案#3或者您也可以通過擴展RoutingExtension類樹枝覆蓋URL功能。
Symfony\Bridge\Twig\Extension\RoutingExtension
可以找到一個例子here與path
但url
應該是相同的。
你應該重寫這個功能
public function getUrl($name, $parameters = array(), $schemeRelative = false)
{
return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
}
你的函數看起來是這樣的:
public function getUrl($name, $parameters = array(), $schemeRelative = false)
{
$yourUrl = parent::getUrl($name, $parameters = array(), $schemeRelative = false);
return strstr($yourUrl, '?' , true);
}
什麼ID OES它消除一切afther問號。
覆蓋您要添加到參數
twig.extension.routing.class: MyNamespace\MyRoutingExtension
不是一個球迷添加的默認類if語句所有的地方,但加入'|狹縫(「?」)| first'到底的鏈接不是太糟糕 – Chausser 2014-09-05 19:15:49