Symfony4: No route found for "GET /lucky/number"
Asked Answered
S

6

7

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

Saadi answered 15/12, 2017 at 20:35 Comment(4)
Have you run composer require annotations?Lxx
I've read just now symfony.com/doc/current/…Saadi
Aaaaaaand. It works!Saadi
Same problem.. Not working though.Uvarovite
A
7

In Symfony4 you have to install annotations bundle.

Run this command composer require annotations

Then restart Your project.

Afra answered 16/12, 2017 at 6:58 Comment(5)
I have the same problem as OP, except running the command "composer require annotations" doesn't solve the issue :/ Exact same error.Epiclesis
@MégaLag, Have restart your project or web server apache?Afra
@ImanaliMamadiev Is it possible that some other changes are needed? I've the exact same problem as OP and doing what you suggested didn't help. Even after restarting everything.Demona
I am also facing the same issue, this solution is mentioned docs (symfony.com/doc/current/page_creation.html) as well. But it is not working. The exception is thrown from the file vendor\symfony\http-kernel\EventListener\RouterListener.php, where there is no mention about annotations.Expenditure
But this is not mentioned here symfony.com/doc/current/controller.html which it definitelly should be. Just doing by the documantion and you are getting error.Yul
E
10

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

Everything answered 19/2, 2019 at 4:8 Comment(0)
A
7

In Symfony4 you have to install annotations bundle.

Run this command composer require annotations

Then restart Your project.

Afra answered 16/12, 2017 at 6:58 Comment(5)
I have the same problem as OP, except running the command "composer require annotations" doesn't solve the issue :/ Exact same error.Epiclesis
@MégaLag, Have restart your project or web server apache?Afra
@ImanaliMamadiev Is it possible that some other changes are needed? I've the exact same problem as OP and doing what you suggested didn't help. Even after restarting everything.Demona
I am also facing the same issue, this solution is mentioned docs (symfony.com/doc/current/page_creation.html) as well. But it is not working. The exception is thrown from the file vendor\symfony\http-kernel\EventListener\RouterListener.php, where there is no mention about annotations.Expenditure
But this is not mentioned here symfony.com/doc/current/controller.html which it definitelly should be. Just doing by the documantion and you are getting error.Yul
I
1

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
Imprinting answered 22/8, 2019 at 12:6 Comment(0)
E
0

As suggested in @Jack70 answer.

https://127.0.0.1:8000/en/random/number

This should work.

If you run the command php bin/console debug:router it would show you the list of routes available as you can see in that list, you need to add the locale (language) as first argument.

app_annotationsample_number ANY ANY ANY /{_locale}/random/number

Expenditure answered 9/10, 2019 at 15:28 Comment(0)
D
0

If you use annotations - you need check config/routes/anotations.yaml. You need check path to your controller or add new path, for example:

controllers:
    resource: ../../src/Controller/
    type: annotation
Dari answered 1/4, 2020 at 7:57 Comment(0)
M
0

Also, you'll get that error if the route name appears more than once.

I ran into that error while developing Angular app, so in order to debug I tried that same route in Postman, which I knew used to work fine, but this time it did not throwing the mentioned error. Luckily, I have just added one new controller, so finding the culprit was easy: the problem was that after copying methods from web controller to api controller I forgot to change route name in one of the methods. So you might wanna be careful when copying :)

Manipulate answered 21/11, 2020 at 22:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.