【问题标题】:Symfony error: Attribute "Symfony\Component\Routing\Annotation\Route" cannot target function (allowed targets: class, method)Symfony 错误:属性 \"Symfony\\Component\\Routing\\Annotation\\Route\" cannot target function (allowed targets: class, method)
【发布时间】:2022-12-05 00:11:01
【问题描述】:

从 Symfony 6.1 升级到 6.2 后,出现此错误:

属性“Symfony\Component\Routing\Annotation\Route”不能以函数为目标(允许的目标:类、方法)

...在这个控制器上:

final class HomepageController extends AbstractController
{
    #[Route(path: '/', name: 'homepage')]
    public function __invoke(): Response
    {
        // ...
    }
}

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    简答

    执行以下操作之一将修复它:

    • 将 PHP 更新到 >= 8.1.10
    • Route 属性从 __invoke 向上移动到 class

    长答案

    Symfony 的Route 属性在类和方法上是允许的,参见Route

    #[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
    

    在 Symfony 6.2 中,属性的读取方式发生了变化:https://github.com/symfony/symfony/pull/46001

    不幸的是,PHP 8.1.6 中引入了一个错误,因此Attribute::TARGET_METHOD 不再涵盖魔术方法__invoke()

    这已在 PHP 8.1.10 中修复:https://github.com/php/php-src/pull/9173

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2021-09-10
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多