我有一个 REST 服务,我想通过 PUT 更新文件。当我使用 POST 时,我使用以下内容来获取上传的文件:
/**
* @var Request $request
*/
$request->files->get('file');
Run Code Online (Sandbox Code Playgroud)
如何在Symfony Framework中将上传的文件作为PUT发送?
我需要在symfony 2上为我的网站实现RESTful API,所以我使用FOSRestBundle + JMSSerializerBundle
我的实体有这样的序列化器yml:
Acme\DemoBundle\Entity\Product:
exclusion_policy: ALL
accessor_order: custom
custom_accessor_order: [id, title]
properties:
id:
expose: true
title:
expose: true
virtual_properties:
getMainPhoto:
serialized_name: photo
Run Code Online (Sandbox Code Playgroud)
问题是getMainPhoto返回我的网址到完整大小的图像.我希望在向api客户端发送响应之前预处理此URL,我可以生成新的url来调整此类映像的大小.我已经在sf2中有服务可以完成这项工作:
$resized_url = $someService->generateResizedUrl($item->getMainPhoto(), 640, 480);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在JMSSerializer中使用此服务.也许在发送响应之前有一些FOSRestBundle\JMSSerializerBundle的回调?
我正在尝试关注如何使用Symfony2设置一个好的REST API的Will Durand教程.但是,当我收到此错误时,我在一开始就失败了:
The controller must return a response (Array(welcome => Welcome to my API) given).
Run Code Online (Sandbox Code Playgroud)
我的基本配置基本必须是错误的.我已经尝试了不同的fos_rest配置设置,但配置参考并没有提供非常有用,因为我真的不明白单个设置的作用.
我的设置:
//config.yml
sensio_framework_extra:
view:
annotations: true
fos_rest: ~
Run Code Online (Sandbox Code Playgroud)
//Controller
<?php
namespace Acme\Bundle\ApiBundle\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
class DefaultController
{
/**
* @Rest\View
*/
public function indexAction()
{
return array(
'welcome' => 'Welcome to my API'
);
}
}
Run Code Online (Sandbox Code Playgroud)
我的API应该基于accept标头返回XML或者JSON.永远不会有html输出.
我正在尝试使用FOSRestBundle的请求体转换器,但我当前的实现似乎不起作用.
我正在使用Symfony2,Propel,AngularJS(它将数据发送到服务器)
我究竟做错了什么?
config.yml:
fos_rest:
routing_loader:
default_format: json
include_format: false
view:
view_response_listener: force
body_listener: true
param_fetcher_listener: true
body_converter:
enabled: true
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
request: { converters: true }
Run Code Online (Sandbox Code Playgroud)
方法:
/**
* @View()
* @Put("/documenttypes")
* @ParamConverter("documentType", converter="fos_rest.request_body")
*/
public function putAction(DocumentType $documentType)
{
print_r($documentType);
return $documentType;
}
Run Code Online (Sandbox Code Playgroud)
结果我有空模型对象:
Backend\SettingsBundle\Model\DocumentType Object
(
[id:protected] =>
[name:protected] =>
....
)
Run Code Online (Sandbox Code Playgroud)
要检查数据是否到达服务器,我修改方法:
public function putAction(DocumentType $documentType)
{
$content = $this->get("request")->getContent();
$documentType->fromArray(json_decode($content, true));
print_r($documentType);
return $documentType;
} …Run Code Online (Sandbox Code Playgroud) 这是我的配置文件:
// app/config/config.yml
fos_rest:
body_listener:
array_normalizer: fos_rest.normalizer.camel_keys
Run Code Online (Sandbox Code Playgroud)
我使用的是最新版本的FOSRestBundle:
// composer.json
"friendsofsymfony/rest-bundle": "dev-master"
Run Code Online (Sandbox Code Playgroud)
这些是我的帖子参数:
// Post parameters
"first_name": "First name",
"last_name": "Last name",
"phone": "Phone"
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
/**
* @ApiDoc(
* resource=true,
* description="Create a new user"
* )
*
* @View()
*/
public function postAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
// Request post parameters are not camel cased
// Parameters expected: firstName, lastName, phone
// Parameters got: first_name, last_name, phone
$form->handleRequest($request);
if ($form->isValid()) {
return …Run Code Online (Sandbox Code Playgroud) 我发现了许多关于使用FOSRest进行部分API响应的问题,所有答案都基于JMS序列化程序选项(exlude,include,groups等).它工作正常,但我试图实现一些不那么"静态"的东西.
假设我有一个具有以下属性的用户: id username firstname lastname age sex
我使用端点GET /users/{id}和以下方法检索此用户:
/**
* @View
*
* GET /users/{id}
* @param integer $user (uses ParamConverter)
*/
public function getUserAction(User $user) {
return $user;
}
Run Code Online (Sandbox Code Playgroud)
该方法返回用户的所有属性.
现在我想允许这样的事情: GET /users/{id}?attributes=id,username,sex
我是否错过了FOSRestBUndle,JMSserializer或SensioFrameworkExtraBundle的功能来自动实现它?请求中的注释,方法,关键字或其他内容?
否则,实现它的最佳方法是什么?
我想做类似的事情:
/**
* @View
* @QueryParam(name="attributes")
*
* GET /users/{id}
*
* @param integer $user (uses ParamConverter)
*/
public function getUserAction(User $user, $attributes) {
$groups = $attributes ? explode(",", $attributes) : array("Default");
$view = $this->view($user, 200)
->setSerializationContext(SerializationContext::create()->setGroups($groups)); …Run Code Online (Sandbox Code Playgroud) 我目前正在使用FOSRESTBundle和JMSSerialize来制作 RESTFull API(当然)。
我的项目是一个供客户和管理员使用的外联网。
这样,我必须禁止客户查看某些字段,仅对管理员可见。
我首先为实体进行了序列化器配置:
AppBundle\Entity\IncidentComment:
exclusion_policy: ALL
properties:
id:
expose: true
groups: [list, details]
author:
expose: true
groups: [list, details]
addedAt:
expose: true
groups: [list, details]
content:
expose: true
groups: [details]
customerVisible:
expose: true
groups: [list_admin, details_admin]
Run Code Online (Sandbox Code Playgroud)
如您所见,customerVisible组有_admin后缀。该字段应仅对管理员显示。
_admin如果用户具有例如 ROLE_ADMIN 角色或其他条件,我想动态添加带有后缀的组来在视图上设置组,而不将其写入每个其余控制器的每个操作上。
我正在考虑创建一个带有安全上下文参数的自定义视图处理程序来添加组,但我不知道这是否是正确的方法。
你认为这是好方法吗?您对此有什么建议吗?
顺便说一句,如果某些开发人员遇到同样的问题,我会很高兴看到他如何解决它!:)
谢谢。
我们使用Symfony2 FOSRestBundle和JMSSerializerBundle来开发移动开发人员使用的REST API.
JSON格式的API响应在适用的情况下返回"null"作为属性的值,这将为移动开发人员使用的第三方库生成例外.
我没有看到JMSSerializerBundle或FOSRestBundle的解决方案根据我们的要求覆盖该值.
到目前为止的解决方法 我可以在实体中设置默认值,以便新数据在数据库中具有一些默认值,而不是null.但这对于一对一/多对一关系对象不起作用,因为默认情况下它们将返回null而不是空白对象.
在序列化后覆盖json的任何解决方案?
问题已解决,请检查我的答案.
我正在我的Symfony2.7 rest api上建立一个注册端点.我正在使用FosRestBundle和FosUserBundle
这是用户模型:
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct() {
parent::__construct();
// your own logic
}
}
Run Code Online (Sandbox Code Playgroud)
\ 这是UserType表单: \
class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email')
->add('username', null)
->add('plainPassword', 'repeated', …Run Code Online (Sandbox Code Playgroud) 我正在使用带有fos-restbundle的Symfony2开发一个应用程序.我想创建一些API路由以及一些常规路由(只有一个用于AngularJS前端).这是我的fos_rest配置(以及来自sensio的一些配置行):
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
request: { converters: true }
fos_rest:
routing_loader:
default_format: json
include_format: true
param_fetcher_listener: force
body_listener: true
allowed_methods_listener: true
view:
view_response_listener: 'force'
formats:
json: true
xml: true
format_listener:
rules:
- { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
access_denied_listener:
json: true
如您所见,我启用了view_response_listener并禁用了注释.我找不到为索引操作定义"常规"(非REST)路由(和视图)的方法(AngularJS的必要).继续收到错误:
ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException: "No matching accepted Response format could be determined" at C:\wamp\www\CRMProject\vendor\friendsofsymfony\rest-bundle\EventListener\FormatListener.php line 69
Run Code Online (Sandbox Code Playgroud)
我很感激任何帮助.