【问题标题】:No route found for /, even though a route is defined即使定义了路由,也没有找到 / 的路由
【发布时间】:2013-06-16 17:27:53
【问题描述】:

当我尝试访问我的 app_dev.php 页面时遇到以下错误:

No route found for "GET /"

问题是我定义了这条路由,我的 routing.yml 文件如下所示:

 0 v42homepage:
 1     pattern:  /
 2     defaults: { _controller: v42Bundle:Default:index }
 3
 4 v42meetTheTeamPage:
 5     pattern:  /meetTheTeam
 6     defaults: { _controller: v42Bundle:Default:meetTheTeam }
 7
 8 v42whatWeDoPage:
 9     pattern:  /whatWeDo
10     defaults: { _controller: v42Bundle:Default:whatWeDo }
11
12 v42contactUsPage:
13     pattern:  /contactUs
14     defaults: { _controller: v42Bundle:Default:contactUs }
15
16 v42homepage:
17     pattern:  /login
18     defaults: { _controller: v42Bundle:Default:login }

这是我的控制器:

<?php
 47
 46 namespace v42\WebsiteBundle\Controller;
 45
 44 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 43
 42 class DefaultController extends Controller
 41 {
 40     public function indexAction()
 39     {
 38         $templating = $this->container->get('templating');
 37
 36         $response = $templating->renderResponse('v42Bundle:Default:index.html.twig');
 35
 34         return $response;
 33     }
 32
 31     public function meetTheTeamAction()
 30     {
 29         $templating = $this->container->get('templating');
 28
 27         $response = $templating->renderResponse('v42Bundle:Default:meetTheTeam.html.twig');
 26
 25         return $response;
 24     }
 23
 22     public function whatWeDoAction()
 21     {
 20         $templating = $this->container->get('templating');
 19
 18         $response = $templating->renderResponse('v42Bundle:Default:whatWeDo.html.twig');
 17
 16         return $response;
 15     }
 14
 13     public function contactUsAction()
 12     {
 11         $templating = $this->container->get('templating');
 10
  9         $response = $templating->renderResponse('v42Bundle:Default:contactUs.html.twig');
  8
  7         return $response;
  6     }
  5
  4     public function loginAction()
  3     {
  2         $templating = $this->container->get('templating');
  1
  0         $response = $templating->renderResponse('v42Bundle:Default:login.html.twig');
  1
  2         return $response;
  3     }
  4
  5 }

我的所有其他路径都可以工作,但在不提供路径的情况下访问 app_dev.php 则不行,即使默认情况下这应该解析为我的 / 路径。这是什么原因造成的?

我已尝试使用 Symfony 的控制台程序清除缓存,如 here 所述。

谢谢

【问题讨论】:

标签: php symfony


【解决方案1】:

以前称为pattern 的内容现在在路由配置中称为path。 旧密钥 pattern 已弃用,将在 Symfony 3.0 中删除。

参考here

如果您对开发环境使用不同的路由,即 routing_dev.yml:

config_dev.yml

framework:
    router:   { resource: "%kernel.root_dir%/config/routing_dev.yml" }

您必须在 routing_dev.yml 中导入生产路线

routing_dev.yml

# ... dev routes here ... i.e. _wdt, _profiler

_main:
    resource: routing.yml

此外,请确保您没有覆盖 routing_dev.yml 中的 / 路由。

第一个匹配的路由模式总是获胜 - 在 routing_dev.yml 中导入 routing.yml之前的路由将被覆盖。

但是 ... 使用相同的路由名称会覆盖之前声明的那些。

您可以使用

检查您的应用程序中存在哪些路由
app/console router:debug

app/console router:debug --env=prod

这里的问题:重复的路线名称

v42homepage:
    pattern:  /

v42homepage:
    pattern:  /login

解决方案

将第二个 v42homepage 重命名为 v42login 以解决问题。

【讨论】:

  • 有趣,这就是我安装的基本包中的默认 routing.yml 文件的布局方式。我已经尝试将模式更改为路径,但我仍然遇到同样的错误。
  • 非常感谢,不胜感激。
  • 我的例子中的配置?没有覆盖 / ?错误消失了? :)
  • 只是重新检查一切。我的 routing_dev 正如你的例子所说。然后我有 2 个路由文件,一个在我的应用程序目录中,另一个在我的网站包中。我的应用程序目录中的 1(在 routing_dev 中引用)引用了我的网站包中的那个。我的网站包中的 1 如问题中所发布。我所有其他路线都可以工作,只是顶部/路线停止工作,大约是我添加其他路线的时候。有点头疼!
  • 在答案中添加了路由检查控制台命令 - 可能有帮助:)
猜你喜欢
  • 1970-01-01
  • 2013-06-26
  • 2023-03-31
  • 1970-01-01
  • 2014-06-22
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 2018-12-07
相关资源
最近更新 更多