我使用composer.phar安装Symfony 2.2.1标准版,然后使用应用程序/控制台实用程序生成"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项目”。消息,并且没有注意到配置错误。
发布于 2013-05-22 06:40:01
下面是我是如何让它工作的。我不知道这是否正确。我必须做两件事。
首先,我将以下代码添加到我的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
如果我做得对,请告诉我。谢谢!
发布于 2013-05-22 22:30:01
首先,您需要将路由信息包含到主路由文件中。否则,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 }如果你有类似的东西,那么路由注解就是not working。您需要将其设置为默认值true。
有关详细信息,请参阅官方文档:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
https://stackoverflow.com/questions/16680501
复制相似问题