【发布时间】: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 中使用特定的捆绑包并像专业人士一样工作?是因为他糟糕的文档还是我的问题?
【问题讨论】:
-
文档很差,但您必须在编辑配置后清除缓存,仅此而已:) 您还可以在网上找到一些 kickstarts,例如查看这个 fos_rest config sonata-project.org/bundles/media/3-x/doc/reference/api.html (只有 fos_rest 部分)
标签: symfony fosrestbundle symfony-3.4