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

Shopware控制器参数解析报错 缺少controller.service_arguments标签

Shopware 控制器依赖注入报错修复

报错根因

  • 自定义服务TryPlugin\Service\ReadingData未在services.xml中注册,Symfony依赖注入容器无法识别该类,无法完成自动注入
  • 注册PageController服务时缺失controller.service_arguments标签,该标签是Symfony支持控制器方法参数依赖注入的必要配置,缺失后容器不会解析方法内的自定义服务参数
  • 附带逻辑错误:ReadingData::readData()方法声明为无返回值、且要求传入Context入参,控制器调用时既未传参,又试图将无返回值的结果直接传入Response响应,依赖问题修复后仍会触发运行错误
  • services.xml中存在无效配置:注册了不存在的TryPlugin\Storefront\Controller\ProductController服务,未对应任何实际类文件

修复步骤

  1. 替换services.xml为以下正确配置,完成服务注册、补齐控制器标签、清理无效配置
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <!-- 注册ReadingData服务,注入产品仓库依赖 -->
        <service id="TryPlugin\Service\ReadingData">
            <argument type="service" id="product.repository"/>
        </service>

        <!-- 注册前台控制器,添加控制器参数注入所需标签 -->
        <service id="TryPlugin\Storefront\Controller\PageController" public="true">
            <call method="setContainer">
                <argument type="service" id="service_container"/>
            </call>
            <tag name="controller.service_arguments"/>
        </service>
    </services>
</container>
  1. 修正ReadingData.php的readData方法,修改返回值类型、返回查询结果,在文件头部引入缺失的类引用
// 头部引入类
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;

// 修正readData方法
public function readData(Context $context): EntitySearchResult
{
    return $this->productRepository->search(new Criteria(), $context);
}
  1. 修正PageController.php的showExample方法,补全上下文入参、调整响应逻辑
/**
 * @Route("/examples", name="examples", methods={"GET"})
 */
public function showExample(ReadingData $ReadingDatan, SalesChannelContext $context): Response
{
    $productData = $ReadingDatan->readData($context->getContext());
    // 测试场景可直接返回JSON格式数据,避免对象直接传入Response触发报错
    return new JsonResponse([
        'product_total' => $productData->getTotal(),
        'product_list' => $productData->getEntities()->jsonSerialize()
    ]);
}
  1. 执行以下命令重载插件配置、清除缓存生效
bin/console plugin:refresh
bin/console cache:clear

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 05:39:18