Symfony2: No route found for “GET /lucky/number”

2019-04-03 12:34发布

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-controller at step Creating a Page: Route and Controller

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

but when I open http://[Server-IP]:8000/app_dev.php/lucky/number is always get a 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

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

10条回答
可以哭但决不认输i
2楼-- · 2019-04-03 13:15

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.

查看更多
Lonely孤独者°
3楼-- · 2019-04-03 13:16

I have just added a

<?php

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

Thanks everybody

查看更多
成全新的幸福
4楼-- · 2019-04-03 13:16

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;
查看更多
手持菜刀,她持情操
5楼-- · 2019-04-03 13:21

Another reason you might get a "No route found for "GET /luck/number" is because you have a tab in front of the @Route annotation. I thought I programmed my code exactly like the example, and was getting this error, but turned out it was the tab.

See the following code below, and notice the first one produces the "No Route found..." exception:

/**
 *  @Route("/lucky/number")
 */

 /**
 * @Route("/lucky/number")
 */
查看更多
再贱就再见
6楼-- · 2019-04-03 13:23

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

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

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.

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

查看更多
我只想做你的唯一
7楼-- · 2019-04-03 13:23

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

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

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

查看更多
登录 后发表回答