我已经遵循了与Symfony安装相关的所有步骤,并尝试了Symfony书籍的示例(由Symfony网站提供).目前我在控制器章节(5)上,我尝试以下代码:
namespace MyBundle\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class HelloController extends Controller
{
public function indexAction($name, Request $request)
{
return $this->redirect($this->generateUrl('front_buy'), 301);
}
public function buyAction(Request $request)
{
return $this->render(
'Hello/buy.html.twig',
array(
'name' => 'Nikos'
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
INFO - Matched route "front_buy" (parameters: "_controller": "MyBundle\FrontBundle\Controller\HelloController::buyAction", "_route": "front_buy")
CRITICAL - Uncaught PHP Exception InvalidArgumentException: "Unable to find template "Hello/buy.html.twig"." at /var/www/projects/symfony/vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php line 128
Run Code Online (Sandbox Code Playgroud)
上下文:{"exception":"Object(InvalidArgumentException)"}
我知道这不是什么花哨的东西,但我不知道如何解决这个问题.
我的视图文件位于以下路径:./src/MyBundle/FrontBundle/Resources/views/Hello/buy.html.twig.
您需要使用以下语法呈现它:
$this->render('AcmeBlogBundle:Blog:index.html.twig')
Run Code Online (Sandbox Code Playgroud)
或者用这种语法
$this->render('@BlogBundle/Blog/index.html.twig')
Run Code Online (Sandbox Code Playgroud)
一个例子:
$this->render('@FrontBundle/Hello/buy.html.twig)
Run Code Online (Sandbox Code Playgroud)
可以在文档中找到更多信息,还可以参考symfonys 应存储的最佳实践模板app/Ressources/views
| 归档时间: |
|
| 查看次数: |
6116 次 |
| 最近记录: |