我创建了一个新的 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: /
MyBlogBundle/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/