2015-03-03 68 views
2

我是新的symfony框架。我试图用FOSRest bundle创建webservices,但是当我试图用json实现一个实体的POST方法时遇到了问题。FOSRest Symfony POST使用json

测试:

public function testJsonPostNewTesteAction(){ 
    $this->client->request(
     'POST', 
     '/api/teste/new', 
     array(), 
     array(), 
     array(
      'Content-Type' => 'application/json', 
      'Accept' => 'application/json' 
     ), 
     '{"Teste":{"title":"O teu title"}}' 
    ); 
    $response = $this->client->getResponse(); 
    $this->assertJsonResponse($response, Codes::HTTP_CREATED); 
} 

控制器:

/** 
* 
* @Config\Route(
*  "/teste/new", 
*  name="postTestNew" 
*) 
* @Config\Method({"POST"}) 
* 
* @param Request $request the request object 
* 
* @return View|Response 
*/ 
public function postNewTeste(Request $request){ 
    return $this->processFormTest($request); 
} 

/** 
* Method for create the process form to Advertisements 
* 
* @param Request $request 
* 
* @return mixed 
*/ 
private function processFormTest(Request $request){ 
    $form = $this->createForm(
     new TesteType(), 
     new Teste() 
    ); 
    $form->bind($request); 
    //$from->handleRequest($request); 
    if ($form->isValid()) { 
     $test = $form->getData(); 
     return $test; 
    } 
    return View::create($form, Codes::HTTP_BAD_REQUEST); 
} 

问题是,当我使用的handleRequest()方法的isValid()因为表单没有提交返回false。所以我尝试将handleRequest更改为绑定方法。在最后一种情况下,方法isValid()返回true,但方法getData()返回一个空对象。

我不知道问题是类型窗体类下面。

类型的表格:

/** 
* The constant name to that type 
*/ 
const TYPE_NAME = "Teste"; 

/** 
* {@inheritdoc} 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options){ 
    parent::buildForm($builder, $options); 
    $builder->add("title", ValidationType::TEXT); 
} 

/** 
* {@inheritdoc} 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver){ 
    $resolver->setDefaults(
     array(
      'csrf_protection' => false, 
      'cascade_validation' => true, 
      'data_class' => 'oTeuGato\DatabaseBundle\Entity\Teste' 
     ) 
    ); 
} 

/** 
* Returns the name of this type. 
* 
* @return string The name of this type 
*/ 
public function getName(){ 
    return AdvertisementType::TYPE_NAME; 
} 

我需要用两种方式张贴实体。任何人为我的问题获得任何东西?

感谢您的耐心等待!

回答

3

得到了同样的问题与形式的结合,我发现解决这个问题的唯一办法就是为空字符串设置为窗体getName功能:

/** 
* Returns the name of this type. 
* 
* @return string The name of this type 
*/ 
public function getName(){ 
    return ''; 
} 

,我会建议使用handleRequest方法时您绑定您的数据,因为bind方法已经过时:

private function processFormTest(Request $request){ 
    $form = $this->createForm(
     new TesteType(), 
     new Teste() 
    ); 

    $from->handleRequest($request); 

    if ($form->isValid()) { 
     $test = $form->getData(); 
     return $test; 
    } 

    return View::create($form, Codes::HTTP_BAD_REQUEST); 
} 

它看起来更像是一个黑客,但好像它是现在唯一的办法: https://github.com/FriendsOfSymfony/FOSRestBundle/issues/585

+0

Ohhh很好......我没有看到这个问题。今天我试试你的解决方案,表单使用表单工厂。谢谢 ;) – Nbento 2015-03-03 15:24:45

0

我不能完全肯定,但我认为这是因为你与发送数据:'Content-Type' => 'application/json'

'Content-Type' => 'application/x-www-form-urlencoded'

或者代替至少我想这就是为什么这个handleRequest没有做它的原因事情。

+0

我尝试这样做! – Nbento 2015-03-03 14:55:27