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条回答
疯言疯语
2楼-- · 2019-04-03 13:25

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-04-03 13:26

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-03 13:31

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

查看更多
来,给爷笑一个
5楼-- · 2019-04-03 13:32

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.

查看更多
登录 后发表回答