为什么我的Symfony路线不起作用


Why is my Symfony route is not working?

我制作了一个新的Symfony2捆绑包并删除了Acme捆绑包。

然后我创建了一个新的控制器(MainController.php(:

<?php
namespace My'BlogBundle'Controller;
class MainController extends Controller
{
    /**
     * @Route("/", name="index")
     * @Template()
     */
    public function indexAction()
    {
        return array();
    }

以及一个简单的视图:(Main/index.html.twig(,它只包含一个hello。我的routing.yml为空。当我运行整个项目时,我得到:

No route found for "GET /"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »

这里出了什么问题,如何解决?

这是我的路由调试:

'Symfony>php app/console router:debug
[router] Current routes
Name                     Method Pattern
_wdt                     ANY    /_wdt/{token}
_profiler_search         ANY    /_profiler/search
_profiler_purge          ANY    /_profiler/purge
_profiler_info           ANY    /_profiler/info/{about}
_profiler_import         ANY    /_profiler/import
_profiler_export         ANY    /_profiler/export/{token}.txt
_profiler_phpinfo        ANY    /_profiler/phpinfo
_profiler_search_results ANY    /_profiler/{token}/search/results
_profiler                ANY    /_profiler/{token}
_profiler_redirect       ANY    /_profiler/
_configurator_home       ANY    /_configurator/
_configurator_step       ANY    /_configurator/step/{index}
_configurator_final      ANY    /_configurator/final

我还清除了缓存,但没有成功。

这是路线。yml:

my_blog:
    resource: "@MyBlogBundle/Resources/config/routing.yml"
    prefix:   /

并且CCD_ 4中的routing.yml为空。

按照routes.yml的设置方式,您正在从捆绑包中请求routing.yml文件。

如果您想使用注释来管理捆绑包中的路由,则必须按照以下方式编写routes.yml

my_blog:
    resource: "@MyBlogBundle/Controller/MainController.php"
    prefix:   /
    type:     annotation

您的控制器需要包含FrameworkExtraBundle:中的Route

<?php
namespace My'BlogBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Route;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Template;
class MainController extends Controller
{
    /**
     * @Route("/", name="index")
     * @Template()
     */
    public function indexAction()
    {
        return array();
    }
}

这假设您已经安装了SensioFrameworkExtraBundle(http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#installation)。

有关管线注释的详细信息:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html

对于其他故障排除程序,如果路由器注释安装正确,但路由不起作用,您可能需要安装

composer require symfony/apache-pack

您还可以用为注释声明路由

blog_index:
    path: /
    defaults: {_controller:BlogBundle:index}

在您的BlogBundle/config/routing.yml 中

在根目录中,设置

blog:
    resource: "@BlogBundle/Resources/config/routing.yml"
    prefix:   /

然后在MainController中,注释应该起作用:

..use Symfony'Component'Routing'Annotation'Route;
       /**
         * @Route("/", name="blogindex")
         */

MainController类添加以下注释:

/**
 * @Route("/")
 */
class MainController extends Controller
{
}

你的问题是你引用的路由是yml而不是注释,如果你想使用注释,你必须在应用程序文件夹的前端路由声明为

post:
    resource: "@AcmeBlogBundle/Controller/PostController.php"
    type:     annotation

在PostController类中定义

use Symfony'Component'Routing'Annotation'Route;
    /**
     * @Route("/")
     */
    class MainController extends Controller
    {
    }

以及功能

    /**
     * @Route("/add", name="article_add")
     */
     public function add(){
        ...
     }

请参阅文档

中的参考资料