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

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

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"错误:

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. class IndexController extends AbstractController
  7. {
  8. #[Route('/index', name: 'app_index')]
  9. public function index(): Response
  10. {
  11. return $this->render('index/index.html.twig', [
  12. 'controller_name' => 'IndexController',
  13. ]);
  14. }
  15. }

只是模板,正如您所见。

这些是路由:

  1. -------------------------- -------- -------- ------ -----------------------------------
  2. Name Method Scheme Host Path
  3. -------------------------- -------- -------- ------ -----------------------------------
  4. _preview_error ANY ANY ANY /_error/{code}.{_format}
  5. _wdt ANY ANY ANY /_wdt/{token}
  6. _profiler_home ANY ANY ANY /_profiler/
  7. _profiler_search ANY ANY ANY /_profiler/search
  8. _profiler_search_bar ANY ANY ANY /_profiler/search_bar
  9. _profiler_phpinfo ANY ANY ANY /_profiler/phpinfo
  10. _profiler_search_results ANY ANY ANY /_profiler/{token}/search/results
  11. _profiler_open_file ANY ANY ANY /_profiler/open
  12. _profiler ANY ANY ANY /_profiler/{token}
  13. _profiler_router ANY ANY ANY /_profiler/{token}/router
  14. _profiler_exception ANY ANY ANY /_profiler/{token}/exception
  15. _profiler_exception_css ANY ANY ANY /_profiler/{token}/exception.css
  16. -------------------------- -------- -------- ------ -----------------------------------

我觉得在安装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:

  1. &lt;?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. class IndexController extends AbstractController
  7. {
  8. #[Route(&#39;/index&#39;, name: &#39;app_index&#39;)]
  9. public function index(): Response
  10. {
  11. return $this-&gt;render(&#39;index/index.html.twig&#39;, [
  12. &#39;controller_name&#39; =&gt; &#39;IndexController&#39;,
  13. ]);
  14. }
  15. }

Just the template, as you can see.

  1. -------------------------- -------- -------- ------ -----------------------------------
  2. Name Method Scheme Host Path
  3. -------------------------- -------- -------- ------ -----------------------------------
  4. _preview_error ANY ANY ANY /_error/{code}.{_format}
  5. _wdt ANY ANY ANY /_wdt/{token}
  6. _profiler_home ANY ANY ANY /_profiler/
  7. _profiler_search ANY ANY ANY /_profiler/search
  8. _profiler_search_bar ANY ANY ANY /_profiler/search_bar
  9. _profiler_phpinfo ANY ANY ANY /_profiler/phpinfo
  10. _profiler_search_results ANY ANY ANY /_profiler/{token}/search/results
  11. _profiler_open_file ANY ANY ANY /_profiler/open
  12. _profiler ANY ANY ANY /_profiler/{token}
  13. _profiler_router ANY ANY ANY /_profiler/{token}/router
  14. _profiler_exception ANY ANY ANY /_profiler/{token}/exception
  15. _profiler_exception_css ANY ANY ANY /_profiler/{token}/exception.css
  16. -------------------------- -------- -------- ------ -----------------------------------

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 文件中:

  1. controllers:
  2. resource:
  3. path: ../../src/Controller/
  4. namespace: App\Controller
  5. 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:

确定