mind2minds/cakephp-rest-api
Composer 安装命令:
composer require mind2minds/cakephp-rest-api
包简介
CakePHP 3 plugin for building REST API services
README 文档
README
This plugin is used to create REST API endpoints.
Requirements
This plugin has the following requirements:
- CakePHP 3.0.0 or greater.
- PHP 5.4.16 or greater.
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require mind2minds/cakephp-rest-api
After installation, Load the plugin
Plugin::load('RestApi', ['bootstrap' => true]);
Or, you can load the plugin using the shell command
$ bin/cake plugin load -b RestApi
Usage
You just need to create your API related controller and extend it to RestApi\Controller\AppController instead of default AppController.
namespace App\Controller; use RestApi\Controller\AppController; /** * Demo Controller */ class DemoController extends AppController { /** * Read contacts or single contact details when id given */ public function contacts($id = null) { $contacts = [ //... ]; $result = []; if(!empty($id)) { if (empty($contacts[$id])) { $this->_error(404, 'Missing Contacts', 'Invalid Id Supplied'); } else { $contact = $contacts[$id]; $result = [ 'Id' => $id, 'type' => 'Contact', '_name' => $contact['name'], '_email' => $contact['email'] ]; } } else { foreach ($contacts as $id => $contact) { $result[] = [ 'Id' => $id, 'type' => 'Contact', '_name' => $contact['name'], '_email' => $contact['email'] ]; } } $this->_createJsonApiResponse($result); } }
You can define your logic in your action function as per your need. For above example, you will get following response in json format (json response as per jsonapi specs),
{
"data": {
"type": "contacts",
"id": "1",
"attributes": {
// ... this contact's attributes
},
"relationships": {
// ... this contact's relationships if any
}
}
}
The URL for above example will be http://yourdomain.com/api/contacts. You can customize it by setting the routes in APP/config/routes.php. Endpoint /api/contacts example is
$routes->connect('/api/contacts', ['plugin' => 'RestApi', 'controller' => 'Demo', 'action' => 'contacts']);
Accept basic http authentication header
e.g. Basic NzQxZjNhOTctZTBjNC00OTFjLWI3MDItY2JlYTA5NzVmODhl this is for default demo api key
Its easy to use :)
Reporting Issues
If you have a problem with this plugin or any bug, please open an issue on GitHub.
mind2minds/cakephp-rest-api 适用场景与选型建议
mind2minds/cakephp-rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mind2minds/cakephp-rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mind2minds/cakephp-rest-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-19