我试图在没有视图的情况下创建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