FOSRestBundle 遇到 Symfony4 问题,204 无内容响应
FOSRestBundle woes with Symfony4, 204 no content response
我已经用 get 动作为 Movie 对象制作了一个 rest 控制器。
我的数据库管理器一部电影 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
...
routes/rest.yaml
movies:
type: rest
resource: App\Controller\MovieController
prefix: /api
packages/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/* }
packages/framework.yaml:
framework:
...
templating: { engines: ['twig'] }
以及以下文件:
Controller/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) {}
}
Entity/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
Warning: ReflectionObject::__construct() expects parameter 1 to be object, null given
2
An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface >must be injected in FOS\RestBundle\View\ViewHandler to render templates.
3
Type error: Argument 2 passed to Twig_Environment::render()
must be of the type array
4
There are no registered paths for namespace "FOSRest".
5
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
我必须将 MovieController 编辑为 return 对象或 return 异常。
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);
}
}
您应该像这样在视图注释参数中添加 populateDefaultVars :
/**
*
* @rest\View(populateDefaultVars=false)
*/
我已经用 get 动作为 Movie 对象制作了一个 rest 控制器。
我的数据库管理器一部电影 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
...
routes/rest.yaml
movies:
type: rest
resource: App\Controller\MovieController
prefix: /api
packages/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/* }
packages/framework.yaml:
framework:
...
templating: { engines: ['twig'] }
以及以下文件:
Controller/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) {}
}
Entity/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
Warning: ReflectionObject::__construct() expects parameter 1 to be object, null given
2
An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface >must be injected in FOS\RestBundle\View\ViewHandler to render templates.
3
Type error: Argument 2 passed to Twig_Environment::render() must be of the type array
4
There are no registered paths for namespace "FOSRest".
5
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
我必须将 MovieController 编辑为 return 对象或 return 异常。
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);
}
}
您应该像这样在视图注释参数中添加 populateDefaultVars :
/** * * @rest\View(populateDefaultVars=false) */