我試圖在沒有視圖的情況下創建Rest API並計劃在角度2應用程序中使用這些API。對此有什麼想法?使用cakePHP創建Rest API無視圖3.5
1
A
回答
0
蛋糕使這非常容易。我學到了一些沒有意見的東西。
設置_serialize變量
$data = ['cheeses' => ['gouda', 'pepper jack', 'cheddar']];
$this->set('responseData', $data);
$this->set('_serialize', 'responseData');
擲壞請求異常和其他網絡相關的異常
蛋糕將呈現漂亮的JSON意見你。
發行和Ajax請求時,設置您接受頭是application/JSON
您可以在Stateless Authentication使用蛋糕前綴API版本
查找您的API
0
在您的AppController.php
中,使用這些參數,您的所有控制器都將在json中呈現
public function beforeRender(Event $event)
{
$this->RequestHandler->renderAs($this, 'json');
$this->response->type('application/json');
$this->set('_serialize', true);
}
0
CakePHP很容易呈現json。
在你的控制器中,看起來像什麼。
protected $responseBody = [];
public function beforeRender(Event $event){
foreach($this->responseBody as $responseKey=>$response){
$this->set($responseKey, $response);
}
$this->set('_serialize', array_keys($this->responseBody));
}
public function initialize()
{
parent::initialize();
$this->RequestHandler->renderAs($this, 'json');
}
public function index(){
$this->request->allowMethod(['get']); // Method like post,get..
$this->responseBody["statusCode"] = 200;
$this->responseBody["statusDescription"] = ''; //You send any text in json.
$this->responseBody["data"] = []; // All data that you can send.
}
對於進一步的信息,你可以看到CakePHP的食譜REST API點擊here
相關問題
- 1. 從現有項目創建CakePHP REST api
- 2. 使用CakePHP創建API
- 3. O365創建使用REST API
- 4. 創建視圖(cakePHP noob)
- 5. 如何在ASP.Net 3.5中使用REST API?
- 6. 使用JIRA REST API創建新用戶
- 7. 創建REST Api bean
- 8. CakePHP使用REST API識別用戶
- 9. 使用rest api創建文章wordpress.com
- 10. Watson RnR - 使用REST API創建Ranker
- 11. 使用REST API創建索引
- 12. 如何使用nodejs創建POST rest api?
- 13. 無法使用UIScrollView創建視圖
- 14. CakePHP - 將ACL與REST API結合使用
- 15. 我無法使用office365 REST API創建郵件對象的ItemAttachment
- 16. Kaa - 無法使用REST api創建配置架構
- 17. Cakephp-問題創建/顯示視圖
- 18. 如何使用Rest API創建使用Rest API時的列表參數
- 19. Windows Azure圖形AD使用Rest API創建組
- 20. 無法通過使用REST模塊的API通過API創建新實體(使用ECK創建)
- 21. 在Confluence中通過Rest Rest API使用Java創建頁面
- 22. 如何使用django-rest-framework創建一個登錄視圖
- 23. 在ATK4中創建REST API?
- 24. NodeJS和Java創建REST API
- 25. 爲EJB創建REST API
- 26. Rest API創建文檔
- 27. 創建REST API資源
- 28. 創建REST客戶端API
- 29. 無法使用REST API
- 30. 無法創建視圖 - BigQuery
開始閱讀說明書? https://book.cakephp.org/3.0/en/development/rest.html – burzum