Symfony4 Annotation routing does not work

15,368

Solution 1

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.

Solution 2

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>

Solution 3

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/.

Solution 4

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

Solution 5

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

Share:
15,368
okey_on
Author by

okey_on

Updated on June 04, 2022

Comments

  • okey_on
    okey_on almost 2 years

    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.)
    
  • okey_on
    okey_on almost 6 years
    Your answers works. Any ideas why the official tutorial use Symfony\Component\Annotation\Route instead of Sensio\Bundle\FrameworkExtraBundle\Configuration\Route?
  • Cerad
    Cerad almost 6 years
    Because the FrameworkExtraBundle is no longer used in S4. I was puzzled you accepted this. A "composer require annotation" was all you really needed.
  • okey_on
    okey_on almost 6 years
    @Cerad I did composer require annotation as instructed by the tutorial. It didn't work
  • Cerad
    Cerad almost 6 years
    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.
  • grantDEV
    grantDEV over 5 years
    I added file in public/ catalog and my application run in prod server ;))
  • BenMorel
    BenMorel almost 5 years
    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!
  • nick
    nick over 4 years
    Installing sensio/framework-extra-bundle creates config/routes/annotation.yaml.
  • dankilev
    dankilev over 4 years
    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.
  • eglease
    eglease over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
  • Peter Mortensen
    Peter Mortensen about 2 years
    Is "apache bundle" the literal name? Or do you mean the "apache bundle" or the "Apache bundle"?
  • Peter Mortensen
    Peter Mortensen about 2 years
    Is "public" literal?
  • Peter Mortensen
    Peter Mortensen about 2 years
    OK, the OP has left the building ("Last seen more than 2 years ago").
  • Sebastian Skurnóg
    Sebastian Skurnóg about 2 years
    @PeterMortensen its not a bundle in technical way ... it's just a "pack" symfony.com/doc/current/setup.html#symfony-packs , I edited original post.