Symfony在控制器中找不到路由。

huangapple go评论98阅读模式
英文:

Symfony does not find routes in Controller

问题

我最近购买了一台新的笔记本电脑,并安装了PHP 8.2.8和Symfony。我以为我可以开始一个新的Symfony 5.4 Web应用程序项目,但每次我启动本地服务器时,它都找不到我的Controller中的路由。

我使用了php bin/console make:controller命令,以前它已经行之有效,但现在似乎无法正常工作。

我尝试在网上寻找答案,但据我所知,没有人遇到过相同的问题。我在我的IndexController中有以下代码,但出现了No route found for "GET http://127.0.0.1:8000/index"错误:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

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

只是模板,正如您所见。

这些是路由:

-------------------------- -------- -------- ------ -----------------------------------
  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
 -------------------------- -------- -------- ------ -----------------------------------

我觉得在安装Symfony时可能出了问题,但我不知道出了什么问题。

英文:

I recently got a new laptop, and after I installed PHP 8.2.8 and Symfony, I thought I'd be able to start a new Symfony 5.4 webapp project but whenever I start the local server it doesn't find the routes in my Controller.

I used the command php bin/console make:controller which has done the trick previously, but now doesn't seem to get it to work.

I've tried looking for answers online but there's no one who has had the same problem, as far as I know. I get the No route found for &quot;GET http://127.0.0.1:8000/index&quot; error while the following code is in my IndexController:

&lt;?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class IndexController extends AbstractController
{
    #[Route(&#39;/index&#39;, name: &#39;app_index&#39;)]
    public function index(): Response
    {
        return $this-&gt;render(&#39;index/index.html.twig&#39;, [
            &#39;controller_name&#39; =&gt; &#39;IndexController&#39;,
        ]);
    }
}

Just the template, as you can see.

-------------------------- -------- -------- ------ -----------------------------------
  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
 -------------------------- -------- -------- ------ -----------------------------------

These are the routes.

I feel like something might've gone wrong during the installation of Symfony, but I have no clue what's gone wrong.

答案1

得分: 0

你应该设置路由器来解析Controller目录中文件的属性。将以下代码粘贴到你的 config/routes/attributes.yaml 文件中:

    controllers:
        resource:
            path: ../../src/Controller/
            namespace: App\Controller
        type: attribute
英文:

You should set up router to parse attributes on files in the Controller directory. Paste this code to your config/routes/attributes.yaml:

<!-- language: lang-yaml -->
controllers:
resource:
path: ../../src/Controller/
namespace: App\Controller
type: attribute

答案2

得分: 0

请确保在每次配置更改后执行 bin/console cache:clear

英文:

Make sure to execute bin/console cache:clear after every configuration change

答案3

得分: 0

我是个傻瓜,忘记了在Symfony中要求注释。
感谢你的帮助,但在几个月的首次设置Symfony后,我完全忘记了最基本的步骤:要求注释。

英文:

I was an idiot, and forgot to require annotations in Symfony.
Thanks for the help, but after months of setting up Symfony for the first time i completely forgot the most basic step: requiring annotations.

huangapple
  • 本文由 发表于 2023年8月4日 05:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定