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

后端 未结 11 1272
北荒
北荒 2021-02-18 14:17

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

相关标签:
11条回答
  • 2021-02-18 15:10

    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

    0 讨论(0)
  • 2021-02-18 15:13

    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.

    0 讨论(0)
  • 2021-02-18 15:13

    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

    0 讨论(0)
  • 2021-02-18 15:13

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

    composer require symfony/apache-pack

    Don't forget to address your Document Root to

    where/is/your/symfony/public

    in apache configuration.

    0 讨论(0)
  • 2021-02-18 15:15

    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")
     */
    
    0 讨论(0)
提交回复
热议问题