【问题标题】:Symfony 3+: How Can I learn to use FOSRestBundle?Symfony 3+:如何学习使用 FOSRestBundle?
【发布时间】:2018-12-14 21:27:30
【问题描述】:

我正在尝试使用 Symfony 3.4 创建一个 REST API,但我发现学习如何使用这个包有些困难。

所以当我尝试看一些教程时,由于bundle的版本转换而出现错误,所以我发现我自己尝试了任何配置复制和粘贴直到它起作用,我觉得很丑。

我试图用 JSON 格式获取电影

在遇到问题时,我收到了这个错误:

There are no registered paths for namespace App
Twig_Error_Loader

我通过将其添加到 config.yaml 来解决它

fos_rest:
    view:
        view_response_listener: force

但是在没有做任何更改的情况下,会出现一个新错误:

An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface must be injected in FOS\RestBundle\View\ViewHandler to render templates.

MoviesController.php:

<?php

namespace AppBundle\Controller;

use FOS\RestBundle\Controller\ControllerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
// use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FOS\RestBundle\Controller\Annotations as Rest;
use AppBundle\Entity\Movie;

class MoviesController extends AbstractController
{
  use ControllerTrait;

  /**
   * @Rest\View(populateDefaultVars=false)
   */
  public function getMoviesAction(){
    $movies = $this->getDoctrine()
        ->getRepository(Movie::class)
        ->findAll();

        // print_r($movies); die;
    return $movies;
  }

}

当我们看到文档时,我问我如何理解这些行:

https://symfony.com/doc/master/bundles/FOSRestBundle/configuration-reference.html

所以我今天的问题是一个基本问题:如何学习在 Symfony 中使用特定的捆绑包并像专业人士一样工作?是因为他糟糕的文档还是我的问题?

【问题讨论】:

标签: symfony fosrestbundle symfony-3.4


【解决方案1】:

由于 Symfony 4(包括 Symfony 3.4,因为它是相同的东西)你应该使用 API Platform 而不是 FOSRestBundle

composer require api

不到两分钟,您的 API 就准备好了。 只需添加

use ApiPlatform\Core\Annotation\ApiResource; 供您使用和

@ORM\Entity 到你的实体注释和咕!!!

更多信息:

【讨论】:

    猜你喜欢
    • 2018-04-20
    • 2018-06-03
    • 2011-02-15
    • 2018-12-22
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    相关资源
    最近更新 更多