Symfony4: No route found for "GET /lucky/number"

sensorario picture sensorario · Dec 15, 2017 · Viewed 7.8k times · Source

I am starting to play with symfony4. I've just created new application and create new LuckyController. It works with routes.yaml configured in this manner:

lucky:
    path: /lucky/number
    controller: App\Controller\LuckyController::number

With the following controller:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class LuckyController
{
    public function number()
    {
        return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
    }
}

But I want to use annotations. So I decided to comment routes.yaml. Following documentation that explain how to create a route in symfony I've made this:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class LuckyController extends Controller
{
    /**
     * @Route("/lucky/number")
     */
    public function number()
    {
        return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
    }
}

No route found

Answer

Bhaskararao Gummidi picture Bhaskararao Gummidi · Feb 19, 2019

Sometimes this problem occurs because of cache.you need to run php bin/console cache:clear.then it will work fine.