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

Shopware 6.4.17中自定义ContextSwitchRoute装饰器报错求助

问题原因及修复方案

错误核心原因

  1. 类型不匹配:你的装饰器类未继承或实现Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute类型,导致注入到ContextController时,无法满足构造函数的类型约束。
  2. 装饰器配置缺失:services.xml中未注入被装饰的原始ContextSwitchRoute服务实例,不符合Shopware/Symfony的装饰器模式要求。

具体修复步骤

1. 修改装饰器类

让装饰器继承原始的ContextSwitchRoute类,同时注入原始服务实例用于代理逻辑:

namespace Lp\PickupPoint\Decorator\Core\System\SalesChannel\SalesChannel;

use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute;
use Shopware\Core\Framework\Validation\DataValidator;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class ContextSwitchRouteDecorator extends ContextSwitchRoute
{
    private ContextSwitchRoute $originalRoute;

    public function __construct(
        ContextSwitchRoute $originalRoute,
        DataValidator $validator,
        SalesChannelContextPersister $contextPersister,
        EventDispatcherInterface $eventDispatcher
    ) {
        parent::__construct($validator, $contextPersister, $eventDispatcher);
        $this->originalRoute = $originalRoute;
    }

    // 重写需要自定义的方法,例如switchContext
    public function switchContext(\Shopware\Core\System\SalesChannel\ContextSwitchRouteRequest $request, \Shopware\Core\System\SalesChannel\SalesChannelContext $context)
    {
        // 插入你的自定义逻辑

        // 调用原始服务的方法
        return $this->originalRoute->switchContext($request, $context);
    }
}

2. 修正services.xml配置

添加被装饰的原始服务实例作为第一个参数(使用inner后缀的服务ID):

<service id="Lp\PickupPoint\Decorator\Core\System\SalesChannel\SalesChannel\ContextSwitchRouteDecorator"
         decorates="Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute"
         public="true">
    <!-- 注入被装饰的原始服务实例 -->
    <argument type="service" id="Lp\PickupPoint\Decorator\Core\System\SalesChannel\SalesChannel\ContextSwitchRouteDecorator.inner" />
    <argument type="service" id="Shopware\Core\Framework\Validation\DataValidator" key="$validator" />
    <argument type="service" id="Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister" key="$contextPersister"/>
    <argument type="service" id="event_dispatcher" key="$eventDispatcher" />
</service>

3. 额外检查

确认Shopware 6.4.17版本中ContextSwitchRoute的构造函数参数与你的装饰器一致,避免因版本更新导致的参数不匹配问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 20:05:32