我只需要支持单一的API格式,即JSON,我不喜欢在我的路由中{_format}.是否可以将其删除?
我正在使用FosRestBundle,我正在使用手动路由声明一个控制器.
namespace Cboujon\PropertyBundle\Controller;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations\Get;
/**
* Property controller.
* @RouteResource("Property")
*/
class PropertyRESTController extends \FOS\RestBundle\Controller\FOSRestController {
/**
*
* @return type
* @Get("/types")
*
*/
public function getTypesAction() {
...
return $this->get('fos_rest.view_handler')->handle($view);
}
}
Run Code Online (Sandbox Code Playgroud)
使用routing.yml
cboujon_property_property_api:
resource: "@CboujonPropertyBundle/Controller/PropertyRESTController.php"
type: rest
prefix: /api
Run Code Online (Sandbox Code Playgroud)
当我提出要求http://localhost:8000/api/properties/types或者http://localhost:8000/api/property/types我得到了
找不到404 - NotFoundHttpException.
但如果http://localhost:8000/api/types它有效!
我需要配置网址http://localhost:8000/api/properties/types.我究竟做错了什么?
更新1
没有Property实体.PropertyController应该执行自定义操作(无CRUD).
更新2
你可以看到git存储库
有没有办法在Symfony(而不是JMSSerializer)的序列化程序组件中使用任何配置或类似的东西设置循环引用限制?
我有一个带有FOSRestBundle的REST应用程序和一些包含其他应该序列化的实体的实体.但我遇到了循环引用错误.
我知道如何设置它:
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getName();
});
Run Code Online (Sandbox Code Playgroud)
但这必须在多个控制器中完成(对我来说是开销).我想在config(.yml)中全局设置它,例如:
framework:
serializer:
enabled: true
circular_limit: 5
Run Code Online (Sandbox Code Playgroud)
找不到序列化程序API参考,所以我想知道它是否可能?
是否有人在控制器中使用put,get,post,delete注释(https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Controller/Annotations/).
我试图像这样使用它,但它仍然需要获取方法.FOSRestBundle中那些注释的目的是什么?
/**
* @Route("/get/{id}", defaults={"_format" = "json"})
* @Post
*/
public function getObject($id) {
$object = $this->getService()->findById($id);
return $object;
}
Run Code Online (Sandbox Code Playgroud) 我用Symfony和FOSRestBundle创建了一个RESTful应用程序.FOSRestBundle使用JMS Seriazlizer将数据序列化为json格式.我有一切都在处理一个小问题.
这是我的Entity类
/**
* Post
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Tomalo\AdminBundle\Entity\PostRepository")
* @ExclusionPolicy("none")
*/
class Post
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
* @Assert\NotBlank()
*/
private $content;
/**
* @var float
*
* @ORM\Column(name="lat", type="float")
* @Assert\NotBlank()
*/
private $lat;
/**
* @var float
*
* @ORM\Column(name="lon", type="float")
* @Assert\NotBlank()
*/
private $lon;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/ …Run Code Online (Sandbox Code Playgroud) 我正在使用FOSRestBundle构建API,并且我正处于需要实现创建包含二进制数据的新实体的处理阶段.
按照发送二进制数据和REST API请求
概述的方法,multipart/form-data由于Base64需要增加约33%的带宽,因此发送数据对于我们的实现感觉最实用.
题
如何配置REST端点以处理请求中的文件,并在发送数据时对JSON编码实体执行验证multipart/form-data?
当刚刚发送原始JSON时,我一直在使用Symfony的表单handleRequest方法来执行针对自定义的验证FormType.例如:
$form = $this->createForm(new CommentType(), $comment, ['method' => 'POST']);
$form->handleRequest($request);
if ($form->isValid()) {
// Is valid
}
Run Code Online (Sandbox Code Playgroud)
我喜欢这种方法的原因是,我可以根据操作是更新(PUT)还是新操作(POST)来更多地控制实体的数量.
我知道Symfony的Request对象处理请求,以前JSON数据将是content变量,但现在键入request->parameters->[form key]文件bag(request->files)中的文件.
我正在使用FOSRestBundle和Symfony 2来实现JSON格式的REST API.
我希望以特定的JSON格式返回所有API异常,如下所示:
{
"success": false,
"exception": {
"exceptionClass": "SomeNastyException",
"message": "A nasty exception occurred"
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
我试图摆弄ExceptionController,但它的逻辑看起来太复杂了,不容易重载.
我有实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;
//...
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
**/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"default"})
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="StorageBundle\Entity\File")
* @ORM\JoinTable(name="users_photos",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="photo_id", referencedColumnName="id", onDelete="CASCADE")}
* )
* @Groups({"default"})
*/
protected $photoFiles;
/**
* @ORM\Column(type="string", length=255, nullable=true, unique=false)
* @Groups({"notification"})
*/
protected $deviceId;
/**
* @ORM\Column(type="string", length=255, nullable=true, unique=false)
* @Groups({"default"})
*/
protected $name;
/**
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Car", mappedBy="driver")
* @Groups({"driver"})
*/ …Run Code Online (Sandbox Code Playgroud) 我在使用Symfony 4.1项目下的FOSRestBundle返回视图时遇到问题.
这是我的控制器的代码:
class NewsController extends FOSRestController
{
public function getNewsAction()
{
$data = ['news1', 'news2'];
$view = $this->view($data, 200);
return $this->handleView($view);
}
}
Run Code Online (Sandbox Code Playgroud)
fos_rest.yaml
fos_rest:
param_fetcher_listener: true
allowed_methods_listener: true
routing_loader: true
view:
view_response_listener: 'force'
format_listener:
rules:
- { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json, html ] }
Run Code Online (Sandbox Code Playgroud)
framework.yaml
framework:
secret: '%env(APP_SECRET)%'
php_errors:
log: true
sensio_framework_extra:
view: { annotations: true }
Run Code Online (Sandbox Code Playgroud)
所以我有非常基本的配置,我仍然会收到这样的错误:
(1/1)RuntimeException必须启用SensioFrameworkExtraBundle视图注释才能使用ViewResponseListener.
我试图删除"view:view_response_listener:'force'",但后来我遇到了这个错误:
必须在FOS\RestBundle\View\ViewHandler中注入Symfony\Bundle\FrameworkBundle\Templating\EngineInterface>的实例才能呈现模板.
我几个小时都在挣扎.是因为Symfony 4 beta状态?或者也许我做错了什么?也许我想念一些家属?我在官方文档中找不到任何有关此问题的帮助.
长话短说:使用FOSRestBundle我试图通过POST调用创建一些实体,或通过PUT修改现有实体.
这里的代码:
/**
* Put action
* @var Request $request
* @var integer $id Id of the entity
* @return View|array
*/
public function putCountriesAction(Request $request, $id)
{
$entity = $this->getEntity($id);
$form = $this->createForm(new CountriesType(), $entity, array('method' => 'PUT'));
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->view(null, Codes::HTTP_NO_CONTENT);
}
return array(
'form' => $form,
);
} //[PUT] /countries/{id}
Run Code Online (Sandbox Code Playgroud)
如果我通过PUT调用/ countries/{id}并传递像{"description":"Japan"}这样的json,它会修改我的国家/地区id = 1,并输入一个空的描述.
相反,如果我尝试使用此方法创建一个新实体:
/**
* Create new Countries (in batch)
* @param Request $request json request
* …Run Code Online (Sandbox Code Playgroud)