2013-12-11 32 views
0

我有一個Symfony2應用程序,我試圖讓RESTful。以REST方式加載TWIG表單客戶端

,我無法弄清楚如何(如果它甚至有可能)

  1. 創建使用$form = $form->createView();
  2. 形式

    我最大的問題,序列化

  3. 它傳遞給客戶
  4. 在客戶端解析它。

我正在使用FOS REST軟件包。

我可以在文檔 here中找到的是如何處理錯誤或渲染它。

如果是相關的,我現在用的是 JMS Serializer Bundle

下面的序列化的是,我傳遞到客戶端的JSON:我用FosRestbundle建立一個

{ 
"info": { 
    "ContactName": "Zane Zorn ", 
    "BusinessName": "Business 164", 
    "JobCategory": "House Sitting", 
    "MajorIntersection": "John St and Jane St", 
    "JobDescription": "A job that needs being done" 
}, 
"form": { 
    "vars": { 
     "value": {}, 
     "attr": [], 
     "id": "LeadResponseForm", 
     "name": "LeadResponseForm", 
     "full_name": "LeadResponseForm", 
     "disabled": false, 
     "multipart": false, 
     "block_prefixes": ["form", "LeadResponseForm", "_LeadResponseForm"], 
     "unique_block_prefix": "_LeadResponseForm", 
     "translation_domain": "messages", 
     "cache_key": "_LeadResponseForm_LeadResponseForm", 
     "read_only": false, 
     "errors": [], 
     "valid": true, 
     "data": {}, 
     "required": true, 
     "label_attr": [], 
     "compound": true, 
     "method": "POST", 
     "action": "", 
     "sonata_admin_enabled": false, 
     "sonata_admin": { 
      "edit": "standard", 
      "inline": "natural", 
      "block_name": false 
     } 
    }, 
    "children": { 
     "ContactName": { 
      "vars": { 
       "value": "", 
       "attr": { 
        "value": "Zane Zorn " 
       }, 
       "id": "LeadResponseForm_ContactName", 
       "name": "ContactName", 
       "full_name": "LeadResponseForm[ContactName]", 
       "disabled": false, 
       "label": "Contact Name", 
       "multipart": false, 
       "block_prefixes": ["form", "text", "_LeadResponseForm_ContactName"], 
       "unique_block_prefix": "_LeadResponseForm_ContactName", 
       "translation_domain": "messages", 
       "cache_key": "_LeadResponseForm_ContactName_text", 
       "read_only": false, 
       "errors": [], 
       "valid": true, 
       "required": true, 
       "label_attr": [], 
       "compound": false, 
       "method": "POST", 
       "action": "", 
       "sonata_admin_enabled": false, 
       "sonata_admin": { 
        "edit": "standard", 
        "inline": "natural", 
        "block_name": false 
       } 
      }, 
      "children": [], 
      "rendered": false 
     }, 
     "Email": { 
      "vars": { 
       "value": "", 
       "attr": { 
        "value": "[email protected]" 
       }, 
       "id": "LeadResponseForm_Email", 
       "name": "Email", 
       "full_name": "LeadResponseForm[Email]", 
       "disabled": false, 
       "label": "Your Email", 
       "multipart": false, 
       "block_prefixes": ["form", "text", "email", "_LeadResponseForm_Email"], 
       "unique_block_prefix": "_LeadResponseForm_Email", 
       "translation_domain": "messages", 
       "cache_key": "_LeadResponseForm_Email_email", 
       "read_only": false, 
       "errors": [], 
       "valid": true, 
       "required": true, 
       "label_attr": [], 
       "compound": false, 
       "method": "POST", 
       "action": "", 
       "sonata_admin_enabled": false, 
       "sonata_admin": { 
        "edit": "standard", 
        "inline": "natural", 
        "block_name": false 
       } 
      }, 
      "children": [], 
      "rendered": false 
     }, 
     "Phone": { 
      "vars": { 
       "value": "", 
       "attr": { 
        "value": "(416)877-3344" 
       }, 
       "id": "LeadResponseForm_Phone", 
       "name": "Phone", 
       "full_name": "LeadResponseForm[Phone]", 
       "disabled": false, 
       "label": "Your Phone", 
       "multipart": false, 
       "block_prefixes": ["form", "text", "_LeadResponseForm_Phone"], 
       "unique_block_prefix": "_LeadResponseForm_Phone", 
       "translation_domain": "messages", 
       "cache_key": "_LeadResponseForm_Phone_text", 
       "read_only": false, 
       "errors": [], 
       "valid": true, 
       "required": true, 
       "label_attr": [], 
       "compound": false, 
       "method": "POST", 
       "action": "", 
       "sonata_admin_enabled": false, 
       "sonata_admin": { 
        "edit": "standard", 
        "inline": "natural", 
        "block_name": false 
       } 
      }, 
      "children": [], 
      "rendered": false 
     }, 
     "Message": { 
      "vars": { 
       "value": "", 
       "attr": { 
        "value": "It looks like your gonna need a new Johnson Rod. That's going to be 500$", 
        "style": "width:500px" 
       }, 
       "id": "LeadResponseForm_Message", 
       "name": "Message", 
       "full_name": "LeadResponseForm[Message]", 
       "disabled": false, 
       "label": "The Message", 
       "multipart": false, 
       "block_prefixes": ["form", "text", "textarea", "_LeadResponseForm_Message"], 
       "unique_block_prefix": "_LeadResponseForm_Message", 
       "translation_domain": "messages", 
       "cache_key": "_LeadResponseForm_Message_textarea", 
       "read_only": false, 
       "errors": [], 
       "valid": true, 
       "required": true, 
       "label_attr": [], 
       "compound": false, 
       "method": "POST", 
       "action": "", 
       "sonata_admin_enabled": false, 
       "sonata_admin": { 
        "edit": "standard", 
        "inline": "natural", 
        "block_name": false 
       } 
      }, 
      "children": [], 
      "rendered": false 
     }, 
     "Send to the Customer": { 
      "vars": { 
       "attr": [], 
       "id": "LeadResponseForm_Send to the Customer", 
       "name": "Send to the Customer", 
       "full_name": "LeadResponseForm[Send to the Customer]", 
       "disabled": false, 
       "multipart": false, 
       "block_prefixes": ["button", "submit", "_LeadResponseForm_Send to the Customer"], 
       "unique_block_prefix": "_LeadResponseForm_Send to the Customer", 
       "translation_domain": "messages", 
       "cache_key": "_LeadResponseForm_Send to the Customer_submit", 
       "clicked": false 
      }, 
      "children": [], 
      "rendered": false 
     }, 
     "_token": { 
      "vars": { 
       "value": "b54d228db0acb06ed5686a6e3dbd7a231ec027e2", 
       "attr": [], 
       "id": "LeadResponseForm__token", 
       "name": "_token", 
       "full_name": "LeadResponseForm[_token]", 
       "disabled": false, 
       "multipart": false, 
       "block_prefixes": ["form", "hidden", "_LeadResponseForm__token"], 
       "unique_block_prefix": "_LeadResponseForm__token", 
       "translation_domain": "messages", 
       "cache_key": "_LeadResponseForm__token_hidden", 
       "read_only": false, 
       "errors": [], 
       "valid": true, 
       "data": "b54d228db0acb06ed5686a6e3dbd7a231ec027e2", 
       "required": false, 
       "label_attr": [], 
       "compound": false, 
       "method": "POST", 
       "action": "", 
       "sonata_admin_enabled": false, 
       "sonata_admin": { 
        "edit": "standard", 
        "inline": "natural", 
        "block_name": false 
       } 
      }, 
      "children": [], 
      "rendered": false 
     } 
    }, 
    "rendered": false 
} 

}

回答

1

寧靜的api。 看看博客文章: http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/

您需要在處理put/post操作時創建表單。 的列表(getResouces動作)和一個獲得一個資源通過其ID(的getResource動作)只需使用倉庫找,但是你必須對控制器的動作休息註釋:

@Rest\View(serializerGroups={"self.link"}) 

而上的路由YML包:

status: 
type: rest 
resource: Radsphere\RecruitmentBundle\Controller\StatusController 
prefix: /api 
name_prefix: api_recruitment_ 

然後,我有一個集中處理表單方法,它處理所有的職位,並把行動

/** 
* Process update or create request from a resource 
* @param $apiResource 
* @return View|Response 
*/ 
protected function processForm($apiResource) 
{ 
    $form = null; 
    try { 
     $resourceData = $apiResource->getData(); 
     $statusCode = $resourceData->getId() ? 204 : 201; 
     $formMethod = "POST"; 
     if ($statusCode === 204) { 
      $formMethod = "PUT"; 
     } 

     $request = $this->getRequest(); 

     $form = $this->createForm(
      $apiResource->getResourceFormName(), 
      $resourceData, 
      array('method' => $formMethod) 
     ); 
     $request = $this->get('api_resource.factory')->formatRequestWithForm(
      $request, 
      $apiResource->getResourceFormName(),$content 
     ); 

     $form->handleRequest($request); 


     if ($form->isValid()) { 


      $this->get($apiResource->getResourceHandlerName())->update($resourceData); 

      $response = new Response(); 
      $response->setStatusCode($statusCode); 

      // set the 'Location' header only when creating new resources 
      if ($statusCode === 201 || $statusCode === 204) { 

         $serializedObject = $this->get('jms_serializer')->serialize($resourceData, 'json'); 

       } 
       $response->setContent($serializedObject); 


      } 

      return $response; 
     } 
     return View::create($form, 400); 
    } catch (\Exception $e) { 
     $this->get('logger')->error("Exception raised during post a resource " . $e->getMessage()); 
     return View::create($e->getMessage(), 400); 
    } 


}