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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31919257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:43:13  来源:igfitidea点击:

Symfony2: No route found for "GET /lucky/number"

phpsymfonysymfony-2.7symfony-routing

提问by Atreides78

I start the tutorial (as newbie) and everythings works fine till:

我开始教程(作为新手)并且一切正常,直到:

http://symfony.com/doc/current/book/page_creation.html#creating-a-page-route-and-controllerat step Creating a Page: Route and Controller

http://symfony.com/doc/current/book/page_creation.html#creating-a-page-route-and-controller在步骤创建页面:路由和控制器

I have created a file called /var/www/html/[projekt]/src/AppBundle/Controller/LuckyController.php

我创建了一个名为 /var/www/html/[projekt]/src/AppBundle/Controller/LuckyController.php

but when I open http://[Server-IP]:8000/app_dev.php/lucky/numberis always get a 404:

但是当我打开http://[Server-IP]:8000/app_dev.php/lucky/number 时总是得到 404:

No route found for "GET /lucky/number"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ?

[2/2] NotFoundHttpException: No route found for "GET /lucky/number"   +
[1/2] ResourceNotFoundException:    +

routing.yml

路由.yml

app: 
    resource: "@AppBundle/Controller/" 
    type: annotation

Controller

控制器

namespace AppBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class LuckyController 
{ 

    /**
     * @Route("/lucky/number") 
     */
    public function numberAction() 
    { 
        $number = rand(0, 100);
        return new Response( '<html><body>Lucky number: '.$number.'</body></html>' ); 
    }
}

No idea where is the mistake...

不知道错在哪里...

ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /lucky/number"" at /var/www/html/[Symfony-Folder]/app/cache/dev/classes.php line 2061

错误 - 未捕获的 PHP 异常 Symfony\Component\HttpKernel\Exception\NotFoundHttpException:“在 /var/www/html/[Symfony-Folder]/app/cache/dev/classes 处找不到“GET /lucky/number”的路由。 php 行 2061

php app/console debug:route
 [router] Current routes
 Name                     Method Scheme Host Path
 _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_purge          ANY    ANY    ANY  /_profiler/purge
 _profiler_info           ANY    ANY    ANY  /_profiler/info/{about}
 _profiler_phpinfo        ANY    ANY    ANY  /_profiler/phpinfo
 _profiler_search_results ANY    ANY    ANY  /_profiler/{token}/search/results
 _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
 _configurator_home       ANY    ANY    ANY  /_configurator/
 _configurator_step       ANY    ANY    ANY  /_configurator/step/{index}
 _configurator_final      ANY    ANY    ANY  /_configurator/final
 _twig_error_test         ANY    ANY    ANY  /_error/{code}.{_format}
 homepage                 ANY    ANY    ANY  /

回答by Atreides78

I have just added a

我刚刚添加了一个

<?php

to the file "LuckyNumberController" and it works.... really strange.

到文件“LuckyNumberController”,它工作......真的很奇怪。

Thanks everybody

谢谢大家

回答by pliski

Try this URL:

试试这个网址:

http://[Server-IP]:8000/app_dev.php/en/lucky/number

http://[Server-IP]:8000/app_dev.php/en/lucky/number

There's a bug in the Symfony Book: they forgot the demo app support i18n.

Symfony Book 中有一个错误:他们忘记了演示应用程序支持 i18n。

Just add the "/en" parameter before the routing ones.

只需在路由参数之前添加“ /en”参数。



I know this question it's closed but I hope my answer can help others that are still facing this problem.

我知道这个问题已经结束,但我希望我的回答可以帮助仍然面临这个问题的其他人。

回答by Apuig

Sometimes is a cache problem. Try to remove devfolder from /project/var/cache/dev.

有时是缓存问题。尝试从 /project/var/cache/dev 中删除dev文件夹。

As authentictechsays, also you can try the command:

正如authentictech所说,您也可以尝试以下命令:

php bin/console cache:clear

php bin/console cache:clear

If you want to specify the environment, you should add the --env=prod property.

如果要指定环境,则应添加 --env=prod 属性。

回答by Dan Mironis

You actually don't extend Symfony Controller class. It should be class LuckyController extends Controller

你实际上没有扩展 Symfony Controller 类。它应该是类 LuckyController extends Controller

// src/AppBundle/Controller/LuckyController.php 
namespace AppBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Component\HttpFoundation\Response;

class LuckyController extends Controller {

     /** 
      * @Route("/lucky/number") 
      */ 
     public function numberAction() { 
         $number = rand(0, 100); 
         return new Response('<html><body>Lucky number: '.$number.'</body></html>');
     }

}

EDIT:after all, the problem in this question was not in extending controller, so ignore my answer.

编辑:毕竟,这个问题的问题不在于扩展控制器,所以忽略我的回答。

回答by Francesco Abeni

I copied and pasted your controller in an existing Symfony project and the route immediately appeared. So nothing wrong with the controller.

我将您的控制器复制并粘贴到现有的 Symfony 项目中,该路由立即出现。所以控制器没有问题。

I also compared the routing.yml and it's the same.

我也比较了routing.yml,结果是一样的。

The only thing that's left is to check the folder structure and make sure you don't have different Symfony projects mixed together; maybe you're editing the right file but starting the web server from a different path.

剩下的唯一事情就是检查文件夹结构并确保您没有将不同的 Symfony 项目混合在一起;也许您正在编辑正确的文件,但从不同的路径启动 Web 服务器。

Check carefully or rebuild the project in a completely different path. Since you're testing with the embedded web server, you don't need to put your project into /var/www/html (in fact it's better not to).

仔细检查或以完全不同的路径重建项目。由于您正在使用嵌入式 Web 服务器进行测试,因此您无需将项目放入 /var/www/html(实际上最好不要这样做)。

回答by Koen Cornelis

In case someone else runs into this problem: for me the problem was twofold:

万一其他人遇到这个问题:对我来说,问题是双重的:

  1. i forgot to assign a namespace namespace AppBundle\Controller;
  2. i didn't add a use statement for the template use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  1. 我忘了分配一个命名空间 namespace AppBundle\Controller;
  2. 我没有为模板添加 use 语句 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

回答by Synexis

Had the same issue, but for a completely different reason than those shown here so far...

有同样的问题,但原因与目前显示的完全不同......

Somehow my demo wasn't defining a default locale. There appear to be a number of configurations that could address this, but I'm not familiar enough with Symfony yet to know the exact cause. My solution until then is to simply define the locale in the URL, for example:

不知何故,我的演示没有定义默认语言环境。似乎有许多配置可以解决这个问题,但我对 Symfony 还不够熟悉,尚不清楚确切原因。在那之前我的解决方案是简单地在 URL 中定义语言环境,例如:

/app_dev.php/en/lucky/number

/app_dev.php/ en/lucky/number

回答by Krishna

Extending the base class Controller did not work for me. To solve the problem I had to do the following change in web/app.php

扩展基类 Controller 对我不起作用。为了解决这个问题,我必须在 web/app.php 中做以下更改

$kernel = new AppKernel('prod', true);

Also I had to add 'en' in the url: http://scotchbox.demo/en/lucky/number

我还必须在网址中添加“en”: http://scotchbox.demo/en/lucky/number

回答by Doc Olive

If you're using apache server with Symfony 4 (not the Symfony server) just add :

如果您在 Symfony 4(不是 Symfony 服务器)中使用 apache 服务器,只需添加:

composer require symfony/apache-pack

作曲家需要 symfony/apache-pack

Don't forget to address your Document Root to

不要忘记将您的文档根地址发送到

where/is/your/symfony/public

哪里/是/你的/symfony/公共

in apache configuration.

在 apache 配置中。

回答by Marco

http://localhost:8080/SymfonyProject/web/app_dev.php/lucky/number

http://localhost:8080/ SymfonyProject/web/app_dev.php/lucky/number

It works for me. Change "SymfonyProject" with your project name folder or delete it if your project is pointed directly by the Server. Also pay attention to the port that works for you (localhost:8080). In my case is 8080, in your could be 8000 or something else.

这个对我有用。使用您的项目名称文件夹更改“SymfonyProject”,或者如果您的项目由服务器直接指向,则将其删除。还要注意适合您的端口 (localhost:8080)。在我的情况下是 8080,在你的情况下可能是 8000 或其他东西。