【问题标题】:Symfony 5 NormalizerInterface not foundSymfony 5 NormalizerInterface 未找到
【发布时间】:2020-06-09 16:44:13
【问题描述】:

我正在关注有关如何使用 Symfony 的在线教程

我遵循了所有步骤,但由于某种原因,当我尝试使用 NormalizeInterface 时,我得到了这个 错误

无法确定“App\Controller\ApiPostController::index()”的控制器参数:$normalizer 参数的类型提示带有不存在的类或接口:“App\Controller\NormalizerInterface”。忘记添加use语句了吗?

我尝试了多种解决方案,但都没有奏效

我的代码是

 <?php

 namespace App\Controller;

 use App\Repository\PostRepository;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;  
 use Symfony\Component\Routing\Annotation\Route;  
 use Symfony\Component\Serializer\Serializer;  
 use Symfony\Component\Serializer\Normalizer;  



class ApiPostController extends AbstractController   
{  
    /**  
     * @Route("/api/post", name="api_post_index", methods={"GET"})  
     */  
    public function index(PostRepository $postRepository, NormalizerInterface  $normalizer    )  
    {
      $posts = $postRepository->findAll();

      $postsNormalises = $normalizer->normalize($posts, null, ['groups' => 'post:read']);

        return $this->render('api_post/index.html.twig', [
            'controller_name' => 'ApiPostController',
        ]);
    }
}

感谢您抽出宝贵时间阅读并提前感谢您的帮助

【问题讨论】:

  • 您可以尝试清除缓存,您可以看到“bin/console debug:container NormalizerInterface”产生了什么。它应该默认设置为序列化器服务,但如果您的教程涉及创建不同的序列化器,那么您需要进行一些调整。

标签: php symfony visual-studio-code


【解决方案1】:

添加使用线:

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

【讨论】:

  • @Cerad 将错误信息读到最后很容易 :) Did you forget to add a use statement?
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 2021-11-20
  • 2013-04-11
  • 2017-06-06
相关资源
最近更新 更多