eugene-khorev/yii2-json-rpc-controller
Composer 安装命令:
composer require eugene-khorev/yii2-json-rpc-controller
包简介
Yii2 JSON RPC 2.0 extension
关键字:
README 文档
README
Change your request component class in configuration to \jsonrpc\Request:
'components' => [ ... 'request' => [ 'class' => 'jsonrpc\Request', 'cookieValidationKey' => '...', ], ... ]
Create a new controller that extends \jsonrpc\Controller:
class SomeController extends \jsonrpc\Controller { /** * This is regular controller action */ public function actionIndex() { return $this->render('index'); } /** * This is JSON-RPC 2.0 controller action */ public function rpcEcho($param1, $param2) { return ['recievedData' => ['param1' => $param1, 'param1' => $param1]]; } }
Now if you post this JSON-RPC 2.0 request to /some/rpc:
{
"jsonrpc": "2.0",
"method": "echo",
"params": {
"param1": "abc",
"param2": "123"
},
"id": 1
}
You get the following result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"recievedData": {
"param1": "abc",
"param2": "123"
}
}
}
And if you navigate your browser to /some/index you get regular \Yii2\web\Controller behavoir.
Advanced usage
Parameters, models and validation
Create a new model:
class SomeModel extends yii\base\Model { public $value1; public $value2; }
Make changes to you RPC controller action so it looks like this:
public function rpcEcho($param1, SomeModel $modelParam) { return ['recievedData' => [ 'param1' => $param1, 'modelParam' => $modelParam->attributes ]]; }
Now if you post this JSON-RPC 2.0 request to /some/rpc:
{
"jsonrpc": "2.0",
"params": {
"param1": "abc",
"modelParam": {
"value1": "123",
"value2": "321",
}
},
"id": 1
}
You get valid $modelParam model in your method. The library uses rpc or default scenario to validate parameter models.
If validation fails a client recieves correct JSON-RPC 2.0 answer containing validation error, but your RPC action method doesn't even run.
Batch requests
You can also use batch JSON-RPC 2.0 requests:
[
{
"jsonrpc": "2.0",
"method": "some-method",
"params": { "something": "anything"},
"id": 1
},
{
"jsonrpc": "2.0",
"method": "another-method",
"params": { "data": [1, 2, 3, 4] },
"id": 2
}
]
In this case two controller methods rpcSomeMethod() and rpcAnotherMethod() will be called sequentially.
And the client will recieve the correct batch results event if there are errors occurred in one of the methods.
eugene-khorev/yii2-json-rpc-controller 适用场景与选型建议
eugene-khorev/yii2-json-rpc-controller 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 180 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rpc」 「json」 「extension」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eugene-khorev/yii2-json-rpc-controller 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eugene-khorev/yii2-json-rpc-controller 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eugene-khorev/yii2-json-rpc-controller 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Microservice RPC through message queues.
Kinikit - PHP Application development framework MVC component
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 180
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-23