简体   繁体   English

Symfony4:找不到“GET /lucky/number”的路由

[英]Symfony4: No route found for "GET /lucky/number"

I am starting to play with symfony4.我开始玩 symfony4。 I've just created new application and create new LuckyController.我刚刚创建了新的应用程序并创建了新的 LuckyController。 It works with routes.yaml configured in this manner:它与以这种方式配置的 routes.yaml 一起使用:

lucky:
    path: /lucky/number
    controller: App\Controller\LuckyController::number

With the following controller:使用以下控制器:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class LuckyController
{
    public function number()
    {
        return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
    }
}

But I want to use annotations.但我想使用注释。 So I decided to comment routes.yaml.所以我决定评论routes.yaml。 Following documentation that explain how to create a route in symfony I've made this:以下文档解释了如何在 symfony 中创建路由,我做了这个:

<?php

namespace App\Controller;

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

class LuckyController extends Controller
{
    /**
     * @Route("/lucky/number")
     */
    public function number()
    {
        return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
    }
}

未找到路线

有时会出现这个问题,因为php bin/console cache:clear 。你需要运行php bin/console cache:clear 。然后它会正常工作。

In Symfony4 you have to install annotations bundle.在 Symfony4 中,您必须安装annotations包。

Run this command composer require annotations运行此命令composer require annotations

Then restart Your project.然后重新启动您的项目。

I had the same thing when trying the annotations, then I found out with the demo installed (the blog) you need to add the language in the URL.我在尝试注释时遇到了同样的事情,然后我发现安装了演示(博客)后,您需要在 URL 中添加语言。 So the documentation says:所以文档说:

http://localhost:8000/lucky/number will work exactly like before http://localhost:8000/lucky/number会像以前一样工作

That did not work.那没有用。 This however did:然而,这确实:

http://localhost:8000/en/lucky/number

As suggested in @Jack70 answer.正如@Jack70 回答中所建议的那样。

https://127.0.0.1:8000/en/random/number https://127.0.0.1:8000/en/random/number

This should work.这应该有效。

If you run the command php bin/console debug:router it would show you the list of routes available as you can see in that list, you need to add the locale (language) as first argument.如果您运行命令php bin/console debug:router它会向您显示可用路由列表,正如您在该列表中看到的那样,您需要添加语言环境(语言)作为第一个参数。

app_annotationsample_number ANY ANY ANY /{_locale}/random/number

If you use annotations - you need check config/routes/anotations.yaml .如果您使用注释 - 您需要检查config/routes/anotations.yaml You need check path to your controller or add new path, for example:您需要检查控制器的路径或添加新路径,例如:

controllers:
    resource: ../../src/Controller/
    type: annotation

Also, you'll get that error if the route name appears more than once.此外,如果路线名称出现多次,您将收到该错误。

I ran into that error while developing Angular app, so in order to debug I tried that same route in Postman, which I knew used to work fine, but this time it did not throwing the mentioned error.我在开发 Angular 应用程序时遇到了那个错误,所以为了调试,我在 Postman 中尝试了相同的路由,我知道它曾经工作正常,但这次它没有抛出提到的错误。 Luckily, I have just added one new controller, so finding the culprit was easy: the problem was that after copying methods from web controller to api controller I forgot to change route name in one of the methods.幸运的是,我刚刚添加了一个新控制器,因此很容易找到罪魁祸首:问题是在将方法从 web 控制器复制到 api 控制器后,我忘记在其中一种方法中更改路由名称。 So you might wanna be careful when copying :)所以你在复制时可能要小心:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM