9

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.)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
okey_on
  • 2,888
  • 8
  • 28
  • 36
  • 2
    have you installed `sensio/framework-extra-bundle`? – Federkun Jun 07 '18 at 19:03
  • @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 Jun 07 '18 at 19:12

9 Answers9

14

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;

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
okey_on
  • 2,888
  • 8
  • 28
  • 36
11

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Julien B.
  • 3,023
  • 2
  • 18
  • 33
  • 1
    I added file in public/ catalog and my application run in prod server ;)) – grantDEV Dec 08 '18 at 00:45
  • Before you check anything else, try this solution! Best suggestion for a new Symfony 4 project, especially when you use "php bin/console make:controller" to create your first controller. – dankilev Oct 24 '19 at 09:45
9

In my case, adding 'symfony/apache-pack' solved the problem:

"composer require symfony/apache-pack"

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

6

I want to give additional advice about annotation errors in Symfony 4:

I handled my issue with that:

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

// config/routes/annotations.yaml

controllers:
    resource: ../../src/Controller/
    type: annotation
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Had the same issue, and adding this file worked for me. I'm not sure why the Flex recipe didn't create this file automatically! – BenMorel Jul 22 '19 at 09:47
  • 2
    Installing `sensio/framework-extra-bundle` creates `config/routes/annotation.yaml`. – nick Aug 06 '19 at 11:36
2

Make sure you install the annotations library with composer require annotations.

This was my issue and not other ones described here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamlet
  • 31
  • 3
1

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

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Preciel
  • 2,666
  • 3
  • 20
  • 45
  • Your answers works. Any ideas why the official tutorial use `Symfony\Component\Annotation\Route` instead of `Sensio\Bundle\FrameworkExtraBundle\Configuration\Route`? – okey_on Jun 07 '18 at 19:48
  • 4
    Because the FrameworkExtraBundle is no longer used in S4. I was puzzled you accepted this. A ["composer require annotation"](https://symfony.com/doc/current/page_creation.html#annotation-routes) was all you really needed. – Cerad Jun 07 '18 at 19:51
  • @Cerad I did `composer require annotation` as instructed by the tutorial. It didn't work – okey_on Jun 07 '18 at 22:42
  • Hmmm. Seems unlikely. I just did a quick install and it works as expected. You really should not have to import the framework extra bundle classes. Just the symfony route like the original question shows. Might want to start a fresh project. – Cerad Jun 08 '18 at 00:20
0

I had the same issue (in my Symfony 5 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 Symfony 4/Symfony 5 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;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ahmadzai
  • 44
  • 5
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mariusz
  • 148
  • 1
  • 8
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30112075) – eglease Oct 19 '21 at 03:44
-2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chrys
  • 47
  • 1
  • 3