php 为什么我的 Symfony 路由不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14427563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my Symfony route is not working?
提问by gurehbgui
I made a new Symfony2 bundle and deleted the Acme bundle.
我制作了一个新的 Symfony2 包并删除了 Acme 包。
Then I created a new Controller (MainController.php):
然后我创建了一个新的控制器(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:
还有一个简单的视图: ( Main/index.html.twig) 只包含一个hello. 我的routing.yml 是空的。当我运行整个项目时,我得到:
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:
这是routes.yml:
my_blog:
resource: "@MyBlogBundle/Resources/config/routing.yml"
prefix: /
and the routing.yml in MyBlogBundle/Resources/config/routing.ymlis empty.
并且routing.yml 中MyBlogBundle/Resources/config/routing.yml是空的。
回答by cheesemacfly
The way your routes.ymlis setup, you are requesting the routing.ymlfile from your bundle.
您routes.yml的设置方式是routing.yml从您的包中请求文件。
If you want to use annotations to manage the routes in your bundle, you have to write the routes.ymlthe following way:
如果你想使用注解来管理你的bundle中的路由,你必须这样写routes.yml:
my_blog:
resource: "@MyBlogBundle/Controller/MainController.php"
prefix: /
type: annotation
And your controller needs to include the Routeclass from the FrameworkExtraBundle:
并且您的控制器需要包含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();
}
}
This assumes you have installed the SensioFrameworkExtraBundle(http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#installation).
这假设您已经安装了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
关于路由注释的更多信息:http: //symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
回答by aurny2420289
You also could declare your routing for your annotation with:
您还可以使用以下命令声明您的注释路由:
blog_index:
path: /
defaults: {_controller:BlogBundle:index}
in your BlogBundle/config/routing.yml
在你的 BlogBundle/config/routing.yml
and in root, set
并在 root 中设置
blog:
resource: "@BlogBundle/Resources/config/routing.yml"
prefix: /
then in your MainController, the annotation should work:
然后在您的 MainController 中,注释应该起作用:
..use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/", name="blogindex")
*/
回答by Sofien Benrhouma
you'r problem is that you have referenced the routing as yml not annotations, if you want to use annotations you must declare at your front route att the app folder as
您的问题是您已将路由引用为 yml 而不是注释,如果您想使用注释,则必须在应用程序文件夹的前端路由中声明为
post:
resource: "@AcmeBlogBundle/Controller/PostController.php"
type: annotation
and at the PostController class you define
并在 PostController 类中定义
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/")
*/
class MainController extends Controller
{
}
and the function
和功能
/**
* @Route("/add", name="article_add")
*/
public function add(){
...
}
See the referance at the docs
请参阅文档中的参考
回答by hsz
Add following annotation for the MainControllerclass:
为MainController该类添加以下注释:
/**
* @Route("/")
*/
class MainController extends Controller
{
}

