【问题标题】:Symfony 6 controller route not found 404 errorSymfony 6 控制器路由未找到 404 错误
【发布时间】:2022-08-24 05:21:32
【问题描述】:

我刚刚安装了一个新的 Symfony 6 框架,并使用php bin/console make:controller 创建了IndexController.php,结果如下:

#[Route(\'/index\', name: \'app_index\')]
public function index(): Response
{
    return $this->render(\'index/index.html.twig\', [
        \'controller_name\' => \'IndexController\',
    ]);
}

但是该路由不起作用并导致 404 错误。我执行了路由调试器,以下输出确认我的路由已被框架识别:

php bin/console debug:router
 -------------------------- -------- -------- ------ -----------------------------------
  Name                       Method   Scheme   Host   Path
 -------------------------- -------- -------- ------ -----------------------------------
  _preview_error             ANY      ANY      ANY    /_error/{code}.{_format}
  _wdt                       ANY      ANY      ANY    /_wdt/{token}
  _profiler_home             ANY      ANY      ANY    /_profiler/
  _profiler_search           ANY      ANY      ANY    /_profiler/search
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open
  _profiler                  ANY      ANY      ANY    /_profiler/{token}
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css
  app_index                  ANY      ANY      ANY    /index
 -------------------------- -------- -------- ------ -----------------------------------
  1. 为什么我的路线不起作用?

  2. 如何配置 Symfony 以生成以下格式的路由注释:

    /**
     * @Route(\"/index\", name=\"app_index\")
     */
    

    标签: routes symfony6


    【解决方案1】:

    添加.htaccess 文件,详见this 帖子有助于解决我的问题。

    我发现没有办法或需要更改路线注释。这似乎是 Symfony 6 中新的和“更好”的格式。

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2021-07-28
      • 2019-05-20
      • 2016-09-11
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多