1
votes
// 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>'
        );
    }
}

I'm new of Symfony framework. I tried this simple code without any result. That's the server response:

No route found for "GET /lucky/number" 404 Not Found - NotFoundHttpException 1 linked Exception: ResourceNotFoundException »

I don't now why the Default controller use the annotation and I can see the homepage of my Symfony application.

2
Are you accessing to /app_dev.php/lucky/number? If you don't, you would need to clear the cache of the production environment: $ php app/console cache:clear --env=prod.D4V1D
another thing: have you imported the routes?Federkun

2 Answers

2
votes

I think you need to import your annotation routes. Symfony will scan locations you mention in app/config/routing.yml.

# app/config/routing.yml
lucky:
    resource: "@AppBundle/Controller/LuckyController.php"
    type:     annotation

See Symfony docs for more details

Also try clearing the cache, just to be sure.

1
votes

You're missing an important part of the route and its name:

/**
* @Route("/lucky/number", name="lucky_number")
*/

More information: http://symfony.com/doc/current/book/routing.html