我最终创建了一个树枝扩展。它冗长,涉及传递 route_params 和 query_params 但它有效。
所以在我的控制器中,我需要获取路由和查询参数:
$routeParams = $this->get('request')->attributes->get('_route_params');
$queryParams = $this->get('request')->query->all();
然后通过查看:
return array('products' => $products, 'mfdFacets' => $mfdFacets, 'routeParams' => $routeParams, 'queryParams' => $queryParams);
然后在我的 browse.html.twig 中调用视图助手并传递路由和查询参数:
{% render controller("MyCoreBundle:Helper:menumfd", {'mfdFacets': mfdFacets, 'routeParams': routeParams, 'queryParams': queryParams }) %}
然后在辅助控制器中:
/**
* @Route("/helper/menu/module/mfd")
* @Template()
*/
public function menumfdAction($mfdFacets, $routeParams, $queryParams) {
$manufacturers = $this->get("my.manufacturers")->makeNamedArray($mfdFacets);
return array('manufacturers' => $manufacturers, 'routeParams' => $routeParams, 'queryParams' => $queryParams);
}
然后在助手视图中:
<ul>
{% for mfd in manufacturers %}
<li><a href="{{ mybrowseroute("brand", mfd.id, "brandName", mfd.name, routeParams, queryParams) }}">{{ mfd.name | raw }} ({{ mfd.count }})</a></li>
{% endfor %}
</ul>
然后是 Twig 扩展(服务?)类:
<?
namespace My\Bundle\ServiceBundle\Twig\Extension;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
class MyRouterExtension extends \Twig_Extension {
protected $container;
public function __construct(Container $container) {
$this->container = $container;
}
public function getFunctions() {
return array(
'mybrowseroute' => new \Twig_Function_Method($this, 'myBrowseRoute')
);
}
public function myBrowseRoute($label, $value, $label2, $value2, $routeParams, $queryParams) {
#print_r($routeParams);
$route_array = array("category", "categoryName", "brand", "brandName");
## Value 1
if (array_key_exists($label, $routeParams)) {
$routeParams["$label"] = $value;
} else {
if (in_array($label, $route_array)) {
$routeParams["$label"] = $value;
}
}
if (array_key_exists($label, $queryParams)) {
$queryParams["$label"] = $value;
} else {
if (!array_key_exists($label, $route_array) && !array_key_exists($label, $routeParams)) {
$queryParams["$label"] = $value;
}
}
## Value 2
if (array_key_exists($label, $routeParams)) {
$routeParams["$label2"] = $value2;
} else {
if (in_array($label, $route_array)) {
$routeParams["$label2"] = $value2;
}
}
if (array_key_exists($label2, $queryParams)) {
$queryParams["$label2"] = $value2;
} else {
if (!array_key_exists($label2, $route_array) && !array_key_exists($label2, $routeParams)) {
$queryParams["$label2"] = $value2;
}
}
## Generate URL string
$base_route = $this->container->get('router')->generate("browse");
$routeString = $base_route;
if (array_key_exists("category", $routeParams)) {
$routeString .= "/c/" . $routeParams["category"];
}
if (array_key_exists("categoryName", $routeParams)) {
$routeString .= "/" . urlencode($routeParams["categoryName"]);
}
if (array_key_exists("brand", $routeParams)) {
$routeString .= "/b/" . $routeParams["brand"];
}
if (array_key_exists("brandName", $routeParams)) {
$routeString .= "/" . urlencode($routeParams["brandName"]);
}
# Page
$routeString .= '/1';
$i = 1;
foreach($queryParams as $qLabel => $qValue){
if($i == 1){
$routeString .= "?$qLabel=$qValue";
} else {
$routeString .= "&$qLabel=$qValue";
}
$i++;
}
return $routeString;
}
public function getName() {
return 'my_router';
}
}
在 routing.yml 中需要以下内容:
browse:
pattern: /browse
defaults: { _controller: MyCoreBundle:Browse:browse }
在 services.yml 中:
services:
my.router:
class: My\Bundle\ServiceBundle\Twig\Extension\MyRouterExtension
arguments: ['@service_container']
tags:
- { name: twig.extension }
如果您只想在视图助手中传递 1 个键值对,您可以使用:
<li><a href="{{ mybrowseroute("brand", mfd.id, '', '', routeParams, queryParams) }}">{{ mfd.name | raw }} ({{ mfd.count }})</a></li>