我使用get动作为Movie对象创建了一个休息控制器。
我的数据库管理员使用id:3
一部电影。
当我尝试访问localhost:8000/api/movie/3
或其他任何想法时,它会直接回到我来自的页面,只有一点没有内容响应。
[Tue May 29 16:06:42 2018] 127.0.0.1:61540 [204]: /api/movie/3
我有以下配置:
services.yaml
services:
...
sensio_framework_extra.view.listener:
alias: Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener
...
路由/ rest.yaml
movies:
type: rest
resource: App\Controller\MovieController
prefix: /api
包/ fos_rest.yaml
fos_rest:
param_fetcher_listener: true
allowed_methods_listener: true
routing_loader:
include_format: false
view:
view_response_listener: 'force'
format_listener:
rules:
- { path: '^/api', priorities: ['json'], fallback_format: 'json' }
zone:
- { path: ^/api/* }
包/ framework.yaml:
framework:
...
templating: { engines: ['twig'] }
以下文件:
控制器/ MovieController.php
<?php
namespace App\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\Controller\FOSRestController;
/**
* @Rest\RouteResource("Movie", pluralize=false)
*/
class MovieController extends FOSRestController implements ClassResourceInterface {
/**
* @Rest\View()
* @Rest\Get("/movie/{id}")
*/
public function getAction(string $id) {}
}
实体/ Movie.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\MovieRepository")
*/
class Movie {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=32)
*/
private $title;
public function __construct($title) {
$this->title = $title;
}
...
}
调试路由器给了我这个结果:
$ bin/console debug:router | grep movie
get_movie GET ANY ANY /api/movie/{id}
[更新]
由于之前的错误配置,我遇到了错误:
1
警告:ReflectionObject :: __ construct()期望参数1为object,给定
为null
2
必须在FOS \ RestBundle \ View \ ViewHandler中注入Symfony \ Bundle \ FrameworkBundle \ Templating \ EngineInterface&gt;的实例才能呈现模板。
3
类型错误:参数2传递给Twig_Environment :: render() 必须是数组类型
4
命名空间“FOSRest”没有注册路径。
5
SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符
答案 0 :(得分:0)
我必须编辑MovieController以返回对象或返回异常。
MovieController.php
<?php
namespace App\Controller;
use App\Repository\MovieRepository;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\Controller\FOSRestController;
/**
* @Rest\RouteResource("Movie", pluralize=false)
*/
class MovieController extends FOSRestController implements ClassResourceInterface {
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var MovieRepository
*/
private $movieRepository;
public function __construct(
EntityManagerInterface $entityManager,
MovieRepository $movieRepository
) {
$this->entityManager = $entityManager;
$this->movieRepository = $movieRepository;
}
public function getAction(string $id) {
$movie = $this->movieRepository->find($id);
if($movie === null) {
throw new NotFoundHttpException();
}
return $this->view($movie);
}
}
答案 1 :(得分:0)
您应该在视图注释参数中添加populateDefaultVars,如下所示:
/ ** * * @rest \ View(populateDefaultVars = false) * /