Symfony 2.4找不到“GET / new”的路线

时间:2013-12-27 06:18:46

标签: symfony-2.3

我为 DemoController 创建了一个新的路由,但它最终以

结束
404 error.
"No route found for "GET/new".

404 Not Found - NotFoundHttpException

1个关联的例外ResourceNotFoundException.如何解决?

以下是代码:

_new:
    resource: "@AcmeDemoBundle/Controller/NewController.php"
    type: annotation
    prefix:  /new

控制器

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class NewController extends Controller
{
    public function indexAction()
    {
        /*
         * The action's view can be rendered using render() method
         * or @Template annotation as demonstrated in DemoController.
         *
         */

        return $this->render('AcmeDemoBundle:New:indexs.html.twig');
    }
}

2 个答案:

答案 0 :(得分:1)

我想我看到了你的错误。 正如Dani在评论中所说,如果将注释设置为类型,则必须在要运行的每个操作上添加@route注释。

所以,你必须这样做:

使用Sensio \ Bundle \ FrameworkExtraBundle \ Configuration \ Route Class 在注释中添加@Route(“/”,name =“new_index”)

以下是您可以使用的代码示例:

<?php

namespace Acme\DemoBundle\Controller;

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

class NewController extends Controller
{
    /**
     * Index action.
     *
     * @return Response
     *
     * @Route("/", name="new_index")
     */
    public function indexAction()
    {
        /*
         * The action's view can be rendered using render() method
         * or @Template annotation as demonstrated in DemoController.
         *
         */

        return $this->render('AcmeDemoBundle:New:indexs.html.twig');
    }
}

还有一件事,我对你的控制器的相关性有了严重的怀疑,特别是对于所选的名字。 “新建”是一个操作名称,控制器的名称应该是您要通过此路径创建的对象的名称。例如,如果你想创建汽车,那就是我写的控制器:

<?php

namespace Acme\DemoBundle\Controller;

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

class CarController extends Controller
{
    /**
     * new action.
     *
     * @return Response
     *
     * @Route("/", name="car_new")
     */
    public function newAction()
    {


        return $this->render('AcmeDemoBundle:Car:new.html.twig');
    }
}

希望它有所帮助! 欢呼声,

答案 1 :(得分:0)

对于routing.yml部分,你很好。 但是如果你想通过CarController示例来遵循我的建议,那么这就是你应该编写的代码:

cars:
    resource: "@AcmeDemoBundle/Controller/CarController.php"
    type: annotation
    prefix:  /cars