You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6.5.0:如何通过事件订阅器获取当前页面ID

在Shopware 6.5.0中获取当前页面ID的方法

一、在事件监听器(StorefrontRenderEvent/PageLoadedEvent)中获取页面ID

1. PageLoadedEvent 系列事件

PageLoadedEvent是抽象类,实际触发的是对应页面类型的具体事件(如ProductPageLoadedEvent、CategoryPageLoadedEvent、CmsPageLoadedEvent等)。直接从事件对象中获取页面实例,调用getId()即可拿到ID:

use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Category\CategoryPageLoadedEvent;
use Shopware\Storefront\Page\Cms\CmsPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class PageIdSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductPageLoadedEvent::class => 'handleProductPage',
            CategoryPageLoadedEvent::class => 'handleCategoryPage',
            CmsPageLoadedEvent::class => 'handleCmsPage',
        ];
    }

    public function handleProductPage(ProductPageLoadedEvent $event): void
    {
        $pageId = $event->getPage()->getId();
        // 此处添加业务逻辑
    }

    public function handleCategoryPage(CategoryPageLoadedEvent $event): void
    {
        $pageId = $event->getPage()->getId();
    }

    public function handleCmsPage(CmsPageLoadedEvent $event): void
    {
        $pageId = $event->getPage()->getId();
    }
}

如果要监听通用的PageLoadedEvent,可以通过判断方法存在性兼容不同页面类型:

use Shopware\Storefront\Page\PageLoadedEvent;

public function handleGenericPage(PageLoadedEvent $event): void
{
    $page = $event->getPage();
    if (method_exists($page, 'getId')) {
        $pageId = $page->getId();
        // 业务逻辑处理
    }
}

2. StorefrontRenderEvent

该事件中可以通过两种方式获取页面ID:

  • 从请求路由参数中提取(不同页面类型的参数名不同)
  • 从渲染参数中直接获取已加载的page对象
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class RenderPageIdSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            StorefrontRenderEvent::class => 'onStorefrontRender',
        ];
    }

    public function onStorefrontRender(StorefrontRenderEvent $event): void
    {
        // 方式1:从请求路由参数获取
        $request = $event->getRequest();
        $productId = $request->get('productId');
        $categoryId = $request->get('categoryId');
        $cmsPageId = $request->get('cmsPageId');

        // 方式2:从渲染参数中获取page对象
        $page = $event->getParameters()['page'] ?? null;
        if ($page && method_exists($page, 'getId')) {
            $pageId = $page->getId();
        }
    }
}

二、在Twig模板中获取页面ID

1. 通用适配方式

多数页面模板中可以直接访问全局的page对象,获取其ID:

{% if page is defined and page.id is defined %}
    当前页面ID:{{ page.id }}
{% endif %}

2. 特定页面类型的直接获取

针对不同页面类型,还可以直接获取关联实体的ID:

  • 商品页:
{{ page.product.id }}
  • 分类页:
{{ page.category.id }}
  • CMS页面:
{{ page.cmsPage.id }}

3. 从路由参数获取

通过app.request对象直接提取路由参数:

{# 商品ID #}
{{ app.request.get('productId') }}

{# 分类ID #}
{{ app.request.get('categoryId') }}

{# CMS页面ID #}
{{ app.request.get('cmsPageId') }}

内容的提问来源于stack exchange,提问作者Radhu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 06:30:06