Reputation: 3006
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.)
Upvotes: 9
Views: 19328
Reputation: 161
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/.
Upvotes: 9
Reputation: 188
If someone has a similar error, he/she can check if he/she wrote "route" correctly. I forgot the closing brace.
Upvotes: 0
Reputation: 44
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;
Upvotes: 0
Reputation: 41
Make sure you install the annotations
library with composer require annotations
.
This was my issue and not other ones described here.
Upvotes: 3
Reputation: 81
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
Upvotes: 6
Reputation: 47
For Symfony 4 with the file .htaccess in the public folder, solve the issue with the Routing Annotation.
Upvotes: -2
Reputation: 3324
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>
Upvotes: 12
Reputation: 3006
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;
Upvotes: 14
Reputation: 2827
Make sure that you've imported the required classes to your controller.
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Upvotes: 1