Shopware 6插件:如何限制StorefrontRenderSubscriber仅作用于列表和搜索页
限制Shopware 6分页插件订阅器的作用范围
我正在开发一款处理分页功能的Shopware 6插件,插件中包含一个StorefrontRenderSubscriber,用于向TWIG模板传递数据。当前该订阅器会在所有页面运行,但Shopware审核团队反馈要求将其限制为仅在需要分页的列表页和搜索页运行。请问如何更新该订阅器以限制其作用范围?
原订阅器代码:
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Shopware\Storefront\Event\StorefrontRenderEvent; use Swag\SwagPagination\TComponents\SwagPaginationData; use Swag\SwagPagination\Components\Helper\LoggerHelper; use Swag\SwagPagination\Service\ConfigService; class StorefrontRenderSubscriber implements EventSubscriberInterface { protected const EXTENSION_NAME = "SwagPagination"; protected ConfigService $configService; protected LoggerHelper $loggerHelper; public function __construct(ConfigService $configService, LoggerHelper $loggerHelper) { $this->configService = $configService; $this->loggerHelper = $loggerHelper; } /** * Get subscribed events for this subscriber. * * @return array The list of subscribed events and their associated methods */ public static function getSubscribedEvents(): array { return [ StorefrontRenderEvent::class => 'onStorefrontRender' ]; } /** * Callback method for the storefront render event. * * @param StorefrontRenderEvent $event The event object * @return void */ public function onStorefrontRender(StorefrontRenderEvent $event): void { try { // Retrieve the current request $request = $event->getRequest(); // Retrieve all query parameters $queryParameters = $request->query->all(); // Create an instance of SwagPaginationData $pluginData = new SwagPaginationData(); // Get the configuration and check if it's active, add queryParameters $config = $this->configService->getConfig(); if ($config && $config->isActive()) { $pluginData->assign([ 'active' => $config->isActive(), 'queryParameters' => $queryParameters, /* * add more fields here */ ]); } // Add the extension to the context $event->getContext()->addExtension(self::EXTENSION_NAME, $pluginData); } catch (\Throwable $e) { // Log any exceptions $this->loggerHelper->logThrowable($e); } } }
解决方案
通过路由名称过滤是最直接且符合Shopware规范的方式,因为列表页、搜索页等需要分页的页面都有固定的路由标识。修改onStorefrontRender方法,在开头添加路由判断逻辑:
public function onStorefrontRender(StorefrontRenderEvent $event): void { try { $request = $event->getRequest(); // 1. 获取当前请求的路由名称 $routeName = $request->attributes->get('_route'); // 2. 定义允许触发订阅器的路由列表(按需调整) $allowedRoutes = [ 'frontend.product.list', // 产品列表页 'frontend.search.page', // 搜索结果页 'frontend.category.page', // 分类列表页 'frontend.sales-channel.product.list' // 多渠道产品列表(如果需要) ]; // 3. 如果当前路由不在允许列表内,直接终止执行 if (!in_array($routeName, $allowedRoutes)) { return; } // 原有的业务逻辑... $queryParameters = $request->query->all(); $pluginData = new SwagPaginationData(); $config = $this->configService->getConfig(); if ($config && $config->isActive()) { $pluginData->assign([ 'active' => $config->isActive(), 'queryParameters' => $queryParameters, /* * add more fields here */ ]); } $event->getContext()->addExtension(self::EXTENSION_NAME, $pluginData); } catch (\Throwable $e) { $this->loggerHelper->logThrowable($e); } }
说明
- 路由名称可根据实际需求调整,比如如果你的插件还需要支持其他分页页面(如博客列表),只需将对应路由添加到
$allowedRoutes数组即可。 - 这种方式避免了订阅器在无关页面执行,减少不必要的性能消耗,同时满足Shopware审核团队的要求。
内容的提问来源于stack exchange,提问作者mlodhi
相关产品推荐
相关产品推荐

