1

我需要覆盖sylius_partial_product_latest路由的行为才能使用附加参数渲染模板。

简单的例子:在不同的页面上,我需要一次显示 2 到 4 个产品,轮播滚动浏览 4-8 个产品。现在在SyliusWebBundle:Frontend/Product:latest模板中我有:

{{ grid(products) }}

但在某些情况下我需要:

{{ grid(products, 2) }}

在其他方面:

{{ grid(products, 3) }}
{# or it can be 4 or any other number #}

现在在模板中我只有productsvar (它由资源机制提供服务),我可以将变量传递到存储库,在那里我可以将它添加到我的products数组中,但这不是一个好方法。它可以通过简单地将变量传递给查询来完成:

{{ render(url('sylius_partial_product_latest', {'size': 3, 'limit': 2, 'template': 'SyliusWebBundle:Frontend/Product:latest.html.twig'})) }}

我需要能够将变量传递size给.SyliusWebBundle:Frontend/Product:latestproducts

我尝试覆盖ProductBundleand ResourceBundle... 但是用自定义操作覆盖控制器(我不想覆盖indexActionor showAction)让我无处可去。PHP 根本没有击中我的控制器。相反,它击中showActionResourceBundle. 而且,是的,我覆盖了路由以指向我的控制器。

如果我用新路由覆盖ProductController并在我的模板中使用它,则会引发异常:

在第 136 行的 SyliusWebBundle:Frontend/Product:show.html.twig 中呈现模板期间引发了异常(“控制器“sylius.controller.product:partialAction” for URI“/partial”不可调用。”) .

当我更改_controller为以标准 Symfony 方式指向我的控制器时,我有下一个异常:

可捕获的致命错误:传递给 Sylius\Bundle\ResourceBundle\Controller\ResourceController::__construct() 的参数 1 必须是 Sylius\Bundle\ResourceBundle\Controller\Configuration 的实例,未给出,在 /Users/mihail/Sites/magazin 中调用/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php 在第 77 行并在 /Users/mihail/Sites/magazin/vendor/sylius/sylius/src/Sylius/Bundle/ResourceBundle/Controller 中定义/ResourceController.php 第 63 行

4

1 回答 1

2

有两种方法可以使用这个..

控制器

您可以像使用任何其他控制器一样扩展控制器并将变量传递到模板中。

要求

您可以通过 GET 参数将变量作为请求传递给您的控制器,就像您的..

{{ render(url('sylius_partial_product_latest', {'size': 3, 'limit': 2, 'template': 'SyliusWebBundle:Frontend/Product:latest.html.twig'})) }}

..然后使用请求对象在您的模板中获取它,例如..

{{ grid(product, app.request.get('size')) }} // with a default or null
于 2014-07-23T07:57:41.323 回答