【问题标题】:Symfony 4 annotation routing does not workSymfony4注释路由不起作用
【发布时间】:2023-03-18 08:52:01
【问题描述】:

我刚开始学习 Symfony。我正在关注this official tutorial。使用config/routes.yaml 完成路由工作正常,但使用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.)

【问题讨论】:

  • 你安装sensio/framework-extra-bundle了吗?
  • @Federkun sensio/framework-extra-bundle 根据我的 composer.json 安装。有没有我应该采取的任何行动,但教程中没有?

标签: symfony routing annotations symfony4


【解决方案1】:

确保您已将所需的类导入控制器。

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

【讨论】:

  • 您的答案有效。任何想法为什么官方教程使用Symfony\Component\Annotation\Route而不是Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
  • 因为 S4 中不再使用 FrameworkExtraBundle。我很困惑你接受了这个。 "composer require annotation" 是您真正需要的。
  • @Cerad 我按照教程的指示做了composer require annotation。它没有工作
  • 嗯。似乎不太可能。我刚刚进行了快速安装,它按预期工作。你真的不应该导入框架额外的捆绑类。就像原始问题所示的 symfony 路线。可能想开始一个新项目。
【解决方案2】:

我发现了我的错误。我使用了错误的命名空间进行路由。

use Symfony\Component\Annotation\Route;

应该是:

use Symfony\Component\Routing\Annotation\Route;

编辑: 我想删除这个问题,但系统不让我删除。

【讨论】:

  • 不要删除它这是一个有用的问题。
  • 为什么总是小事绊倒我们?感谢分享。
【解决方案3】:

我在标准 apache web 服务器上的第一个 Symfony 4 项目遇到了同样的问题。

在我的公用文件夹中创建一个 .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>

【讨论】:

  • 我在 public/ 目录中添加了文件,我的应用程序在 prod 服务器中运行;))
  • 在检查其他任何内容之前,请尝试此解决方案!新 Symfony 4 项目的最佳建议,尤其是当您使用“php bin/console make:controller”创建您的第一个控制器时。
【解决方案4】:

就我而言,添加“apache bundle”解决了这个问题:

"composer require symfony/apache-pack"

如果你通过 /public/ 在浏览器中运行 symfony,你需要这个。

【讨论】:

    【解决方案5】:

    Symfony 4 使用公用文件夹中的 .htaccess 文件,解决路由注释问题。

    【讨论】:

      【解决方案6】:

      我想就 symfony4 中的注释错误提供更多建议:

      我处理了我的问题:

      我的项目没有 config/routes/annotation.yaml,所以创建该文件并写下这些行:

      // config/routes/annotations.yaml
      
      controllers:
          resource: ../../src/Controller/
          type: annotation
      

      【讨论】:

      • 有同样的问题,添加这个文件对我有用。我不确定为什么 Flex 配方没有自动创建这个文件!
      • 安装sensio/framework-extra-bundle会创建config/routes/annotation.yaml
      【解决方案7】:

      确保您使用composer require annotations 安装annotations 库 这是我的问题,而不是这里描述的其他问题。

      【讨论】:

        【解决方案8】:

        我遇到了同样的问题(在我的 Symfony5 项目中),但我的错误是使用单引号而不是双引号来表示路由和路由名称。有时小错误会浪费你很多时间。顺便说一句,在 SF4/SF5 中我们应该避免使用 FrameworkExtraBundle 的路由。

        Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
        

        我们应该使用 symfony 组件(路由/注释)。

        use Symfony\Component\Routing\Annotation\Route;
        

        【讨论】:

          【解决方案9】:

          如果有人有类似的错误,他可以检查他是否正确地写了“路线”。我忘记了右括号。

          【讨论】:

          猜你喜欢
          • 2018-10-17
          • 1970-01-01
          • 2019-10-06
          • 2022-08-14
          • 2023-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多