8
votes

I just started learning Symfony. I am following this official tutorial exactly. Routing works fine when done with config/routes.yaml but on using annotations:

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Annotation\Route;

class LuckyController
{

    /**
     *  @Route("/lucky/number")
     */
    public function number(){

        $number = mt_rand(0, 100);

        return new Response(
            '<html><body><h1>MyLucky Number: ' . $number . '</h1></body></html>'
        );
    }
}

I get this error:

    Exception thrown when handling an exception
(Symfony\Component\Config\Exception\FileLoaderLoadException: [Semantical Error]
 The annotation "@Symfony\Component\Annotation\Route" in method 
App\Controller\LuckyController::number() does not exist, or could not be auto-loaded
 in C:\wamp\vhosts\mysymfony4\config/routes\../../src/Controller/ (which is
 being imported from "C:\wamp\vhosts\mysymfony4\config/routes/annotations.yaml"). Make sure
 annotations are installed and enabled.)
9
have you installed sensio/framework-extra-bundle? - Federkun
@Federkun sensio/framework-extra-bundle is installed, according to my composer.json. Is there any action I'm supposed to take with it that's missing form the tutorial? - okey_on

9 Answers

13
votes

I found out my mistake. I used the wrong namespace for routing.

use Symfony\Component\Annotation\Route;

It should have been:

use Symfony\Component\Routing\Annotation\Route;

EDIT: I wanted to delete this question but the system wouldn't let me.

10
votes

I had the same problem with my first Symfony 4 project on a standard apache web server.

Creating a .htaccess file in my public folder fixed the issue.

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>
9
votes

In my case, adding 'apache bundle' solved the problem:

"composer require symfony/apache-pack"

You need this if you run symfony in browser via /public/.

5
votes

I want to give additional advice about annotation errors in symfony4:

I handled my issue with that:

My project doesn't have config/routes/annotation.yaml, so create that file and write these lines:

// config/routes/annotations.yaml

controllers:
    resource: ../../src/Controller/
    type: annotation
2
votes

Make sure that you've imported the required classes to your controller.

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
1
votes

Make sure you install annotations library with composer require annotations This was my issue and not other described here.

0
votes

I had the same issue (in my Symfony5 project), but my mistake was using single quote instead of double quotes for the route and route name. Sometimes small silly mistakes will waste a lot of your time. By the way, in SF4/SF5 we should avoid using Routing of FrameworkExtraBundle.

Sensio\Bundle\FrameworkExtraBundle\Configuration\Route

We should use symfony component (Routing/Annotation).

use Symfony\Component\Routing\Annotation\Route;
0
votes

If someone has a similar error, he can check if he wrote "route" correctly. I forgot the closing brace.

-2
votes

Symfony 4 with the file .htaccess in public folder, solve the issue with the Routing Annotation.