Symfony2: No route found for "GET /lucky/number"
Asked Answered
E

11

22

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  /
Einkorn answered 10/8, 2015 at 12:13 Comment(13)
Please share your routing.yml and your controllerIsopleth
routing.yml (/app/config/routing.yml): ` app: resource: "@AppBundle/Controller/" type: annotation `Einkorn
Controller: /src/AppBundle/Controller/LuckyController.php ` // 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 { /** * @Route("/lucky/number") */ public function numberAction() { $number = rand(0, 100); return new Response( '<html><body>Lucky number: '.$number.'</body></html>' ); } }`Einkorn
Don't post your files in the comments. Edit your post and add your code.Orthogenesis
Make sure your bundle uses annotationsMoray
@MarcelBurkhard Is there a way to find out if it uses annotations? Sorry I am a realy newbie in Symfony.Einkorn
open app/config/routing.yml and add type: annotation where your bundles is configured. Then clear cache.Moray
Also you might need to enable it on a "global" basis, if it isn't already: symfony.com/doc/current/book/validation.html#configurationMoray
@MarcelBurkhard: Both already configured in the way you descriebed it.Einkorn
Add to LuckyController.php <?phpAllspice
Add some regular yml routes in the same routing.yml and see if they show up.Cask
Does adding a route name make any difference?Vacillatory
@Rhim: Just try your proposal and it works. Thanks everyone.Einkorn
E
22

I have just added a

<?php

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

Thanks everybody

Einkorn answered 11/8, 2015 at 6:4 Comment(3)
Well... that's not strange at all. If there was no opening php tag in the file - there was no php code in the file either, and no annotation to be found and parsed.Hotbox
I have to admit it's an odd error in symfonys part, those tags aren't in the example and we don't know what's opening that file so we can't know if they are necessary or not... perhaps it has to do with how php is configured too... I dont knowIncapacious
This problem is answered in symfony's github Missing php header in the first script src/AppBundle/Controller/LuckyController.php. They refused to add a hint for newbees because they think this is a basic knowledge for php coder. I wasted 2 hr for this.Nashner
A
9

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.

Audiovisual answered 2/11, 2015 at 4:24 Comment(1)
thank you - same problem in the docs for version 5.4Sorcery
S
5

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.

Servais answered 8/2, 2016 at 6:59 Comment(3)
This solved the same problem for me using the php bin/console cache:clear command in the console.Falcongentle
You're point is ok, because the command removes the dev environment by default. With your permission, I've modified the comment to add the command as a possible solution. Thanks!Servais
The clear cache command is a must know, yet they don't post it anywhere in the startup documentation. Note: This still works with Symfony 4.Spectator
S
1

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.

Spondylitis answered 10/8, 2015 at 12:30 Comment(10)
I have added "extends Controller" in the file rc/AppBundle/Controller/LuckyController.php - but I still get the same error...Einkorn
I see... Try to clear cache, by typing 'php app/console cache:clear' command in your console, hope it will help.Spondylitis
already tried What can be the problem at this very basic step? symfony.com/doc/current/book/… - just follow the instructionsEinkorn
Hmm, everything seems to be fine, did you tried 'php app/console debug:router' to see what routes currently exists in your application?Spondylitis
You don't actually need to extend the framework's Controller class.Hoisch
@DanMironis: Added the output to the description of my problemEinkorn
@Einkorn You know what, I followed the instructions and everything is fine to me, I am sure that the problem is in caching, try to delete entirely the dev folder in app/cache folder.Spondylitis
@DanMironis Did the following steps: php app/console cache:clear Clearing the cache for the dev environment with debug true And created a new projekt via "symfony new ...:" but leads again to the problem :(Einkorn
Ok, php app/console cache:clear is fine, but I noticed that it not always helps to clean the cache, so instead try to delete directory 'dev' in 'app/cache', and dont worry, symfony is smart enough to recreate that directory, and it will be cleaned from old cached filed.Spondylitis
@DanMironis: ` [root@symfony cache]# rm -rf dev/ [root@symfony cache]# ls -alh total 4.0K drwxrwxrwx 3 501 games 32 Aug 10 15:52 . drwxr-xr-x 6 501 games 4.0K Aug 10 15:23 .. -rw-r--r-- 1 501 games 0 Jul 31 15:45 .gitkeep drwxr-xr-x 5 apache apache 130 Aug 10 15:35 prod ` But after that still the same problems... ;(Einkorn
E
0

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

Effete answered 10/8, 2015 at 16:46 Comment(0)
S
0

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.

Shutz answered 11/1, 2016 at 15:32 Comment(0)
R
0

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")
 */
Racy answered 7/2, 2016 at 6:39 Comment(1)
This issue at least seems to have been fixed.Kibitz
F
0

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;
Fanning answered 4/3, 2016 at 19:55 Comment(0)
H
0

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

Hill answered 11/6, 2016 at 19:47 Comment(0)
K
0

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

Kellen answered 31/5, 2017 at 11:30 Comment(0)
S
0

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.

Savanna answered 12/11, 2019 at 2:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.