php - 为什么我的 Symfony 路由不起作用?

标签 php symfony

我创建了一个新的 Symfony2 包并删除了 Acme 包。

然后我创建了一个新的 Controller (MainController.php):

<?php
namespace My\BlogBundle\Controller;

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

        return array();
    }

还有一个简单的 View :(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

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

这是 routes.yml:

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

MyBlogBu​​ndle/Resources/config/routing.yml 中的 routing.yml 为空。

最佳答案

routes.yml 的设置方式是从您的包中请求 routing.yml 文件。

如果你想使用注解来管理包中的路由,你必须按以下方式编写 routes.yml:

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

并且您的 Controller 需要包含 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

关于php - 为什么我的 Symfony 路由不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14427563/

相关文章:

PHP nodeValue 剥离 html 标签 - innerHTML 替代方案?

php - 我已经从数据库创建了这个表。现在如果有人更新了表中的值,它如何反射(reflect)回数据库中?

symfony - Sonata - 缺少用户安全状态标签

php - Symfony2 - 奏鸣曲和 KnpMenu - setCurrentUri

symfony - Easy Admin 3 (Symfony 4) OneToOne 关系中的 AssociationField 显示已经关联的实体

php - cURL 响应 header

php - 在 MySQL 中 INSERT INTO ... ON DUPLICATE KEY UPDATE 的正确语法是什么?

php - 使用 PHP 内置 ltrim() 删除单个字符

php - 使用数据库+ Symfony 2 和 Doctrine 中的值填充表单中的选择列表

php - 如何在 Sonata Admin Bundle 中翻译标签