Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 760
sensorario Avatar asked Dec 15 '17 20:12

sensorario


3 Answers

In Symfony4 you have to install annotations bundle.

Run this command composer require annotations

Then restart Your project.

like image 29
Imanali Mamadiev Avatar answered Oct 03 '22 07:10

Imanali Mamadiev


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

like image 103
Bhaskararao Gummidi Avatar answered Oct 03 '22 08:10

Bhaskararao Gummidi


I had the same thing when trying the annotations, then I found out with the demo installed (the blog) you need to add the language in the URL. So the documentation says:

http://localhost:8000/lucky/number will work exactly like before

That did not work. This however did:

http://localhost:8000/en/lucky/number
like image 39
Jacco van der Post Avatar answered Oct 03 '22 09:10

Jacco van der Post