Symfony 4 注释路由不起作用
Symfony 4 annotation routing does not work
我刚开始学习 Symfony。我正在关注 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>'
);
}
}
我收到这个错误:
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.)
确保您已将所需的 类 导入控制器。
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
我发现了我的错误。我为路由使用了错误的命名空间。
use Symfony\Component\Annotation\Route;
应该是:
use Symfony\Component\Routing\Annotation\Route;
我在标准 Apache Web 服务器上的第一个 Symfony 4 项目遇到了同样的问题。
在我的 public 文件夹中创建一个 .htaccess 文件解决了这个问题。
<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>
就我而言,添加 'symfony/apache-pack' 解决了问题:
"composer require symfony/apache-pack"
如果你通过 /public/.
在浏览器中 运行 Symfony,你需要这个
对于 public 文件夹中文件 .htaccess 的 Symfony 4,使用路由注释解决问题。
我想就 Symfony 4 中的注释错误提供额外的建议:
我用那个解决了我的问题:
我的项目没有文件 config/routes/annotation.yaml,因此创建该文件并写入以下行:
// config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
确保使用 composer require annotations
安装 annotations
库。
这是我的问题,而不是此处描述的其他问题。
我遇到了同样的问题(在我的 Symfony 5 项目中),但我的错误是对路由和路由名称使用单引号而不是双引号。有时愚蠢的小错误会浪费你很多时间。顺便说一下,在 Symfony 4/Symfony 5 中我们应该避免使用 FrameworkExtraBundle 的路由。
Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
我们应该使用 symfony 组件 (Routing/Annotation)。
use Symfony\Component\Routing\Annotation\Route;
如果有人有类似的错误,he/she可以检查一下he/she是否写对了“route”。我忘了右大括号。
我刚开始学习 Symfony。我正在关注 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>'
);
}
}
我收到这个错误:
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.)
确保您已将所需的 类 导入控制器。
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
我发现了我的错误。我为路由使用了错误的命名空间。
use Symfony\Component\Annotation\Route;
应该是:
use Symfony\Component\Routing\Annotation\Route;
我在标准 Apache Web 服务器上的第一个 Symfony 4 项目遇到了同样的问题。
在我的 public 文件夹中创建一个 .htaccess 文件解决了这个问题。
<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>
就我而言,添加 'symfony/apache-pack' 解决了问题:
"composer require symfony/apache-pack"
如果你通过 /public/.
在浏览器中 运行 Symfony,你需要这个对于 public 文件夹中文件 .htaccess 的 Symfony 4,使用路由注释解决问题。
我想就 Symfony 4 中的注释错误提供额外的建议:
我用那个解决了我的问题:
我的项目没有文件 config/routes/annotation.yaml,因此创建该文件并写入以下行:
// config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
确保使用 composer require annotations
安装 annotations
库。
这是我的问题,而不是此处描述的其他问题。
我遇到了同样的问题(在我的 Symfony 5 项目中),但我的错误是对路由和路由名称使用单引号而不是双引号。有时愚蠢的小错误会浪费你很多时间。顺便说一下,在 Symfony 4/Symfony 5 中我们应该避免使用 FrameworkExtraBundle 的路由。
Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
我们应该使用 symfony 组件 (Routing/Annotation)。
use Symfony\Component\Routing\Annotation\Route;
如果有人有类似的错误,he/she可以检查一下he/she是否写对了“route”。我忘了右大括号。