Symfony控制器教程错误

时间:2015-01-05 22:08:35

标签: symfony twig

我正在学习如何使用Symfony(http://symfony.com/doc/current/quick_tour/the_controller.html),但是在完成“路线参数”部分并​​转到localhost/hello/fabien后,我收到了:

404 Not Found在此服务器上找不到请求的URL / hello / fabien。”。

但是,我看不出我做错了什么 - 下面是我的代码:

config.yml

# ...
framework:
        templating:
        {engines: ['twig','php']}

DefaultController:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction()
    {
       /** return new Response('Welcome to Symfony!');*/
          return $this->render('default/index.html.twig');
    }


    /**
     * @Route("/hello/{name}", name="hello")
     */
    public function helloAction($name)
    {
        return $this->render('default/hello.html.twig', array(
            'name' => $name
        ));
    }
}

base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {% block body %}{% endblock %}
        {% block javascripts %}{% endblock %}
    </body>
</html>

hello.html.twig

{# app/Resources/views/default/hello.html.twig #}
{% extends 'base.html.twig' %}

{% block body %}
    <h1>Hi {{ name }}! Welcome to Symfony!</h1>
{% endblock %}

2 个答案:

答案 0 :(得分:0)

错误消息是不言自明的。代码应该是这样的:

 /**
 * @Route("/hello/{name}", name="hello")
 */
public function helloAction($name)
{
    return $this->render('AppBundle:default:hello.html.twig', array(
        'name' => $name
    ));
}

或使用注释简化:

 /**
 * @Route("/hello/{name}", name="hello")
 * @Template()
 */
public function helloAction($name)
{
    return array(
        'name' => $name
    );
}

答案 1 :(得分:0)

在我的情况下,我刚刚删除了{%extends'mybase.html.twig'%}并运行了它开始工作的代码,然后我又把它放在同一个地方,它正在运行。它似乎可能是一些缓存问题。所以我也要求你清除缓存。

由于 Rikkin