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

Shopware 5控制器无法找到模板问题求助

Shopware控制器调用模板无法显示的排查方案

已完成services.xml中控制器与订阅器注册,模板文件夹名称与控制器名称lorem_questions一致,但模板无法正常显示,结合你的代码片段,可从以下几点逐一排查:

一、控制器动作与模板文件的匹配规则

Shopware模板遵循模块/控制器/动作.tpl的命名逻辑:

  • 控制器LoremQuestions对应文件夹lorem_questions
  • indexAction对应模板文件:Resources/views/frontend/lorem_questions/index.tpl
  • loremQuestionsPrinterAction对应模板文件:Resources/views/frontend/lorem_questions/lorem_questions_printer.tpl(驼峰动作名需转为下划线格式)

确保模板文件名与动作完全匹配,否则无法自动加载。

二、修复控制器动作的代码错误

你的loremQuestionsPrinterAction中使用die()会直接终止程序,模板没有渲染机会,需删除die:

public function loremQuestionsPrinterAction()
{
    $this->View()->assign("name", 2);
}

三、验证订阅器的模板目录注入

  1. 检查services.xml中是否定义了%lorem_questions.plugin_dir%参数,需在配置顶部添加:
<parameters>
    <parameter name="lorem_questions.plugin_dir">%kernel.project_dir%/custom/plugins/LoremQuestions</parameter>
</parameters>

未定义该参数会导致订阅器无法获取正确的插件目录,模板路径失效。

  1. 可在订阅器方法中添加日志验证路径:
public function addTemplateDir()
{
    $templateDir = $this->pluginDirectory . '/Resources/views/';
    Shopware()->Container()->get('logger')->info('Added template dir: ' . $templateDir);
    $this->templateManager->addTemplateDir($templateDir);
}

查看var/log/目录下的日志,确认路径是否正确存在。

四、修正模板语法错误

第二个模板存在HTML标签闭合错误:

<!-- 错误写法 -->
<div class="tab--header&gt;&quot;&gt;

<!-- 修正后 -->
<div class="tab--header">

Smarty语法错误会直接导致模板渲染失败,需排查所有标签和语法问题。

五、清除缓存确保配置生效

执行Shopware缓存清理命令,确保控制器和订阅器的配置加载生效:

bin/console cache:clear

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 06:55:45