我制作了一个新的Symfony2包并删除了Acme包.
然后我创建了一个新的Controller(MainController.php):
<?php
namespace My\BlogBundle\Controller;
class MainController extends Controller
{
/**
* @Route("/", name="index")
* @Template()
*/
public function indexAction()
{
return array();
}
Run Code Online (Sandbox Code Playgroud)
并且一个简单的视图:( Main/index.html.twig)其中只包含一个hello.我的routing.yml是空的.当我运行整个项目时,我得到:
No route found for "GET /"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »
Run Code Online (Sandbox Code Playgroud)
这里有什么问题以及如何解决?
这是我的路由调试:
\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
Run Code Online (Sandbox Code Playgroud)
我也清除了缓存但没有成功.
这是routes.yml:
my_blog:
resource: "@MyBlogBundle/Resources/config/routing.yml"
prefix: /
Run Code Online (Sandbox Code Playgroud)
并且routing.yml in MyBlogBundle/Resources/config/routing.yml为空.
您routes.yml的设置方式是routing.yml从捆绑包中请求文件.
如果要使用注释来管理捆绑中的路由,则必须routes.yml按以下方式编写:
my_blog:
resource: "@MyBlogBundle/Controller/MainController.php"
prefix: /
type: annotation
Run Code Online (Sandbox Code Playgroud)
并且您的控制器需要包含以下Route类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();
}
}
Run Code Online (Sandbox Code Playgroud)
这假设您已安装SensioFrameworkExtraBundle(http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#installation).
有关路径注释的更多信息:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html