@Route指令不起作用 - 我做错了什么?

时间:2013-05-21 22:23:23

标签: symfony routing url-routing symfony-2.2

我使用composer.phar安装Symfony 2.2.1标准版,然后使用app / console实用程序生成“ClientBundle”。

我正在尝试使用@Route注释定义我的路线。这是我的控制器:

namespace ScrumBoard\ClientBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class DefaultController extends Controller
{
    /**
     * @Route("/my/route/path")
     */
    public function indexAction($name)
    {
        return $this->render('ScrumBoardClientBundle:Default:index.html.twig', array('name' => $name));
    }
}

我的捆绑包的定义如下:

public function registerBundles()
{
    $bundles = array(
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Symfony\Bundle\AsseticBundle\AsseticBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new JMS\AopBundle\JMSAopBundle(),
        new JMS\DiExtraBundle\JMSDiExtraBundle($this),
        new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
        new Acme\HelloBundle\AcmeHelloBundle(),
        new ScrumBoard\ClientBundle\ScrumBoardClientBundle(),
    );

    if (in_array($this->getEnvironment(), array('dev', 'test'))) {
        $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    }

    return $bundles;
}

您可以看到我的捆绑包列表中包含SensioFrameworkExtraBundle。

然而,当我去http://symfony2.localhost/app_dev.php/my/route/path时,我得到了

ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /my/route/path"" at C:\webdev\scrum-whiteboard\symfony-quickstart\app\cache\dev\classes.php line 3609

所以,显然我错过了一些东西......我如何让@Route注释工作?

JFYI,如果我去http://symfony2.localhost/config.php我可以看到Symfony2正在运作。我得到了“欢迎来到你的新Symfony项目”。消息,并且没有注明配置错误。

2 个答案:

答案 0 :(得分:0)

以下是我如何使用它。我不知道这是否正确。我不得不做两件事。

首先,我将此添加到我的routing.yml文件中:

ScrumBoardClientBundle:
  resource: "@ScrumBoardClientBundle/Controller/"
  type:     annotation
  prefix:   /scrum

然后我在控制器的顶部添加了这个“use”语句:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

一旦我这样做,我的路线就开始起作用了。我确实需要添加/ scrum到我的路径,所以我的完整路径变成了:

http://symfony2.localhost/app_dev.php/scrum/my/other/path

请告知我是否已正确完成此操作。谢谢!

答案 1 :(得分:0)

首先,您需要将路由信息包含在主路由文件中。否则,Symfony将不会对您的路由注释有任何了解。这是通过使用与包含其他路由文件相同的方式完成的:

# app/routing.yml
acme_demo:
    resource: @AcmeDemoBundle/Controller/DemoController.php
    type: annotation

注意类型'annotation',这意味着Symfony将查找@Route注释。您还可以使用@AcmeDemoBundle/Controller/作为资源,一次包括捆绑包的所有控制器。


此外,注释只是在注释中初始化的类。因此,您需要使用use语句导入正确的命名空间:

// src/Acme/DemoBundle/Controller/DemoController.php

// ...
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

// ...

最后,请确保您没有关闭路由注释。这可以通过以下方式完成:

# app/config.yml
sensio_framework_extra:
    router: { annotations: false }

如果你有类似的东西,那么路由注释无法正常工作。您需要将其设置为默认值true


有关详细信息,请参阅官方文档:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html