在 symfony 中找不到 PHP 模板 [英] PHP templates not found in symfony
问题描述
我是 symfony 的新手,我想使用 PHP 模板引擎.以下是我为使其工作而遵循的步骤.
I am new to symfony and I want to use PHP templating engine. Below are the steps I followed to make it work.
1.在 config.yml 中启用 PHP 模板引擎
templating:
engines: ['twig', 'php']
2.在routing.yml中定义我的控制器路径和默认值
hello:
path: /hello/{name}
defaults: { _controller: AppBundle:Hello:index, name:World }
3.创建HelloController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class HelloController extends Controller
{
public function indexAction($name)
{
return $this->render('hello/name.html.php', array(
'name'=>$name
));
}
}
4.创建一个视图\app\Resources\views\Hello\name.html.php
<!-- app/Resources/views/Hello/name.html.php -->
Hello <?php echo $name ?>!
但是当我尝试访问 http://127.0.0.1:8000/hello 时,它显示我下面的错误
but when I try to access http://127.0.0.1:8000/hello, it shows me below error
模板hello/name.html.php"不存在.
The template "hello/name.html.php" does not exist.
500 内部服务器错误 - InvalidArgumentException
500 Internal Server Error - InvalidArgumentException
我将模板文件夹名称从Hello"更改为hello",但仍然出现相同的错误.还尝试渲染这样的模板
I changed template folder name from 'Hello' to 'hello' but still same error. Also tried to render template like this
return $this->render(
'AppBundle:Hello:index.html.php',
array('name' => $name)
);
但不走运,我一定在这里遗漏了一些东西.有人可以指导我走向正确的方向吗?
but no luck, I must be missing something here. Can someone please guide me to right direction?
注意: hello/name.html.twig 正在加载,没有任何错误
Note: hello/name.html.twig is loading without any error
谢谢!
推荐答案
如果你的模板在 app/Resources/views/Hello/文件夹中,你应该使用这个符号:
If your template is in app/Resources/views/Hello/ folder, you should use this notation:
$response = $this->render(':Hello:name.html.php');
如果您要将其移动到 src/AppBundle/Resources/views/Hello 文件夹,请使用
If you would move it to src/AppBundle/Resources/views/Hello folder, then use
$response = $this->render('AppBundle:Hello:name.html.php');
这篇关于在 symfony 中找不到 PHP 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!