Symfony4 Annotation routing does not work
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.
okey_on
Updated on June 04, 2022Comments
-
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 almost 6 yearsYour answers works. Any ideas why the official tutorial use
Symfony\Component\Annotation\Route
instead ofSensio\Bundle\FrameworkExtraBundle\Configuration\Route
? -
Cerad almost 6 yearsBecause 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 almost 6 years@Cerad I did
composer require annotation
as instructed by the tutorial. It didn't work -
Cerad almost 6 yearsHmmm. 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 over 5 yearsI added file in public/ catalog and my application run in prod server ;))
-
BenMorel almost 5 yearsHad 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 over 4 yearsInstalling
sensio/framework-extra-bundle
createsconfig/routes/annotation.yaml
. -
dankilev over 4 yearsBefore 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 over 2 yearsThis 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 about 2 yearsIs "
apache bundle
" the literal name? Or do you mean the "apache
bundle" or the "Apache bundle"? -
Peter Mortensen about 2 yearsIs "public" literal?
-
Peter Mortensen about 2 yearsOK, the OP has left the building ("Last seen more than 2 years ago").
-
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.