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

半世苍凉 提交于 2019-12-03 23:50:53
Atreides78

I have just added a

<?php

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

Thanks everybody

Try this URL:

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.

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


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

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

As authentictech says, also you can try the command:

php bin/console cache:clear

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

You actually don't extend Symfony Controller class. It should be class 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.

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

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.

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")
 */

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;

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:

/app_dev.php/en/lucky/number

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!