生成 Symfony 控制器时未找到"GET /Index"路由



我正在使用generate:controller在我的Symfony 3应用程序中创建一个新的控制器。但是没有找到路线。

这是命令的输入/输出...

First, you need to give the controller name you want to generate.
You must use the shortcut notation like AcmeBlogBundle:Post
Controller name: ApplicationSonataPageBundle:Page
Determine the format to use for the routing.
Routing format (php, xml, yml, annotation) [annotation]: yml
Determine the format to use for templating.
Template format (twig, php) [twig]:
Instead of starting with a blank controller, you can add some actions now. An action
is a PHP function or method that executes, for example, when a given route is matched.
Actions should be suffixed by Action.

New action name (press <return> to stop adding actions): IndexAction
Action route [/Index]:
Template name (optional) [ApplicationSonataPageBundle:Page:index.html.twig]:
New action name (press <return> to stop adding actions):

  Summary before generation

You are going to generate a "ApplicationSonataPageBundle:Page" controller
using the "yml" format for the routing and the "twig" format
for templating
Do you confirm generation [yes]?

...这是新控制器类的内容:

namespace ApplicationSonataPageBundleController;
use SymfonyBundleFrameworkBundleControllerController;
class PageController extends Controller
{
    public function IndexAction()
    {
        return $this->render('ApplicationSonataPageBundle:Page:index.html.twig', array(
            // ...
        ));
    }
}

...未调用。相反,我在浏览器中收到以下错误:

找不到" get/index"的路线

在看到我的动作之前,我需要做其他步骤吗?

我尝试过的是:到目前为止,我已经尝试了其他所有类型的路由(XML,PHP,YML(而没有成功。

有几件事要检查:

确保您的路线包含在config/routes.yml

https://symfony.com/doc/3.3/routing.html
https://symfony.com/doc/3.3/routing.html#loading-routes
https://symfony.com/doc/3.3/routing/external_resources.html

还确保您的捆绑包已加载在内核中:app/AppKernel.php

https://symfony.com/doc/3.3/page_creation.html#bundles-configuration

我注意到的另一件事是,您的行动/路线已大写。我不确定这是否效果,但是常见的做法是camelCasing,因此您将拥有indexAction而不是IndexAction

还有一个命令可以显示可用的路由,我相信它像php bin/console routes:debug一样,如果您运行php bin/console,则应显示您可用命令

的价值:我最终放弃并复制以下内容:

_index:
    path:     /Index
    defaults: { _controller: ApplicationSonataPageBundle:Page:Index }

...从生成的YML文件中进入我的应用程序的主routing.yml文件。

最新更新