Symfony2:找不到"GET/lucky/number"的路线

Atr*_*s78 22 php symfony symfony-routing symfony-2.7

我开始教程(作为新手),并且每件事都可以正常工作,直到:

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

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

但是当我打开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:    +
Run Code Online (Sandbox Code Playgroud)

使用routing.yml

app: 
    resource: "@AppBundle/Controller/" 
    type: annotation
Run Code Online (Sandbox Code Playgroud)

调节器

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>' ); 
    }
}
Run Code Online (Sandbox Code Playgroud)

不知道错误在哪里......

错误 - 未捕获的PHP异常Symfony\Component\HttpKernel\Exception\NotFoundHttpException:"在/ var/www/html/[Symfony-Folder]/app/cache/dev/classes中找不到"GET/lucky/number"的路由. php line 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  /
Run Code Online (Sandbox Code Playgroud)

Atr*_*s78 22

我刚添加了一个

<?php
Run Code Online (Sandbox Code Playgroud)

到文件"LuckyNumberController",它的工作....真的很奇怪.

谢谢大家

  • 嗯......这一点都不奇怪.如果文件中没有打开php标签 - 文件中也没有php _code_,也没有找到和解析的注释. (10认同)
  • 这个问题在symfony的github [第一个脚本src/AppBundle/Controller/LuckyController.php中缺少php标题](https://github.com/symfony/symfony-docs/issues/6116)中得到了解答.他们拒绝为newbees添加提示,因为他们认为这是php编码器的基本知识.我浪费了2个小时. (2认同)

pli*_*ski 6

试试这个网址:

HTTP:// [服务器IP]:8000/app_dev.php/EN /吉利/数

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

只需在路由选项之前添加" / en "参数即可.


我知道这个问题已经结束但我希望我的回答可以帮助那些仍然面临这个问题的人.


Apu*_*uig 5

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

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

php bin/console cache:clear

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