douchun1948 2013-01-20 18:09
浏览 64
已采纳

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

I made a new Symfony2 bundle and deleted the Acme bundle.

Then I created a new Controller (MainController.php):

<?php
namespace My\BlogBundle\Controller;

class MainController extends Controller
{
    /**
     * @Route("/", name="index")
     * @Template()
     */
    public function indexAction()
    {

        return array();
    }

And a simple view: (Main/index.html.twig) which only contains a hello. My routing.yml is empty. When I run the whole project I get:

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

What is wrong here and how to solve it?

Here is my routing debug:

\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

I also cleared the cache with no success.

Here is the routes.yml:

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

and the routing.yml in MyBlogBundle/Resources/config/routing.yml is empty.

  • 写回答

4条回答 默认 最新

  • 普通网友 2013-01-20 21:17
    关注

    The way your routes.yml is setup, you are requesting the routing.yml file from your bundle.

    If you want to use annotations to manage the routes in your bundle, you have to write the routes.yml the following way:

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

    And your controller needs to include the Route class from the FrameworkExtraBundle:

    <?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();
        }
    }
    

    This assumes you have installed the SensioFrameworkExtraBundle (http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#installation).

    More information on the route annotation: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么