【问题标题】:symfony 3.2.4 ... Unable to find template "OCPlatformBundle:Advert:index.html.twigsymfony 3.2.4 ... 找不到模板“OCPlatformBundle:Advert:index.html.twig
【发布时间】:2023-03-30 19:04:01
【问题描述】:

我正在使用 symfony 3.2.4(通过控制台 --version 获得)。 我正在学习开放课堂教程。 该控制器始终返回以下错误。 然而,下面的文件存在并且可以访问。

src/OC/PlatformBundle/Resources/views/Advert/index.html.twig

我为此发疯了。有人有想法吗?

提前致谢。

<?php
// src/OC/PlatformBundle/Controller/AdvertController.php
namespace OC\PlatformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class AdvertController extends Controller
{
  public function indexAction()
  {
    $content = $this->get('templating')->render('OCPlatformBundle:Advert:index.html.twig');

    return new Response($content);
  }
}

找不到模板“OCPlatformBundle:Advert:index.html.twig” (查看:/var/www/html/symfony/app/Resources/views, /var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form)。

*

【问题讨论】:

  • 这几乎就像你的包没有在AppKernel注册,但这没有意义,不是吗?
  • 别忘了清理缓存! (twig 必须通过 symfony 编译为 php 并放入缓存)
  • 是的,气泡已注册并且控制器正在工作,同时它返回字符串响应而不是渲染内容。我通过控制台清理缓存(这做得很好,因为我必须在 /var/dev 文件夹中替换 777)但这并没有改变......:/

标签: symfony templates


【解决方案1】:

您是否尝试在 app/Resources/views/Advert/index.html.twig 中创建模板?

这是一个更好的做法:Store all your application's templates in app/Resources/views/ directory.(来自 SF doc)...

所以,你可以通过这种方式“调用”它:

/***/
public function indexAction()
{
    return $this->render('Advert/index.html.twig', []);
}
/***/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多