sprintcube/cakephp-rest
Composer 安装命令:
composer require sprintcube/cakephp-rest
包简介
Rest API plugin for CakePHP 3
README 文档
README
This plugin simplifies the REST API development for your CakePHP 3 application. It simply converts the output of your controller into a JSON response.
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require sprintcube/cakephp-rest
After installation, Load the plugin
Plugin::load('Rest', ['bootstrap' => true]);
Or, you can load the plugin using the shell command
$ bin/cake plugin load -b Rest
Usage
No major change requrires in the way you code in your CakePHP application. Simply, just add one parameter to your route configuration isRest like,
$routes->connect('/foo/bar', ['controller' => 'Foo', 'action' => 'bar', 'isRest' => true]);
And extend your controller to RestController and everything will be handled by the plugin itself. For example,
<?php namespace App\Controller; use Rest\Controller\RestController; /** * Foo Controller * */ class FooController extends RestController { /** * bar method * * @return Response|void */ public function bar() { $bar = [ 'falanu' => [ 'dhikanu', 'tamburo' ] ]; $this->set(compact('bar')); } }
And that's it. You will see the response as below.
{
"status": "OK",
"result": {
"bar": {
"falanu": [
"dhikanu",
"tamburo"
]
}
}
}
Doesn't it too simple? Whatever viewVars you set from your controller's action using set() method, will be converted into JSON response.
Response Format
This plugin returns the response in the following format.
{
"status": "OK",
"result": {
...
}
}
The status key may contain OK or NOK based on your response code. For all successful responses, the code will be 200 and the value of this key will be OK.
In case of error or exception, the value of status will become NOK. Also, based on your application's debug setting, it will contain the exception and trace data.
The result key contains the actual response. It holds all the variables set from your controller. This key will not be available in case of error/exception.
Require Authentication??
This plugin also provides an option to authenticate request using JWT. Simply, just add one more parameter to your route configuration requireAuthorization like,
$routes->connect('/foo/bar', ['controller' => 'Foo', 'action' => 'bar', 'isRest' => true, 'requireAuthorization' => true]);
Now, the plugin will check for the JWT token in the request in form of a header, query parameter or post data. If you want to pass the token in the header, use the following format.
Authorization: Bearer [token]
And for query parameter or post data, use token parameter and set the token as a value of the parameter.
Generate a token
If you require the authentication in API, you first must grant the token to the user who is making the API request. In general, when a user logs in, the response should contain the token for all next requests.
To generate a token, use the method from Utility class of the plugin: JwtToken::generate().
/** * login method * * @return Response|void */ public function login() { // you user authentication code will go here, you can compare the user with the database or whatever $payload = [ 'id' => "Your User's ID", 'other' => "Some other data" ]; $token = \Rest\Utility\JwtToken::generate($payload); $this->set(compact('token')); }
And it will return the token in response. So, in next API calls, a user can use that token for authorization. You can add whatever data is required in your payload.
By default, the plugin uses the predefined key and algorithm to generate JWT token. You can update this configuration by creating config/rest.php file. The content of this configuration file will be as following,
<?php return [ 'Rest' => [ 'jwt' => [ 'key' => 'PUT YOUR KEY HERE', // it should contain alphanumeric string with symbols 'algorithm' => 'HS256' // See https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40 ] ] ];
Access token data
If there is a valid token available in the request, you can access it in your controller using the token and payload properties.
/** * view method * * @return Response|void */ public function view() { $token = $this->token; $payload = $this->payload; // your action logic... }
These properties are also available in your controller's beforeFilter method, so you can put additional authentication logic there.
Reporting Issues
If you have a problem with this plugin or found any bug, please open an issue on GitHub.
sprintcube/cakephp-rest 适用场景与选型建议
sprintcube/cakephp-rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.79k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2018 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「cakephp」 「REST API」 「CakePHP Rest」 「CakePHP Rest Api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sprintcube/cakephp-rest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sprintcube/cakephp-rest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sprintcube/cakephp-rest 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Email Toolkit Plugin for CakePHP
Rhino, a CMS and Application-Framework plugin for CakePHP
A CakePHP plugin for sending mail via SparkPost's REST API
统计信息
- 总下载量: 6.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-20