Shopware 6.4.17中自定义ContextSwitchRoute装饰器报错求助
问题原因及修复方案
错误核心原因
- 类型不匹配:你的装饰器类未继承或实现
Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute类型,导致注入到ContextController时,无法满足构造函数的类型约束。 - 装饰器配置缺失:
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
相关产品推荐
相关产品推荐

