georgique / yii2-jsonrpc

此包的最新版本(2.0.1)没有可用的许可证信息。

Yii2 JSON-RPC 服务器实现

安装次数: 12,229

依赖者: 0

建议者: 0

安全: 0

星标: 9

关注者: 2

分支: 7

开放问题: 2

类型:yii2-extension

2.0.1 2022-03-27 09:35 UTC

This package is auto-updated.

Last update: 2024-09-11 09:54:36 UTC


README

Yii2 JSON-RPC 服务器实现扩展。它不是作为远程过程调用,而是作为远程操作调用,因此将所有 Yii2 功能留给您的服务。

Build Status

功能

  • 利用了完整的 Yii2 功能,因为方法字符串被转换成了路由。
  • 轻量级。
  • 完全符合 JSON-RPC 2.0

用法

入口点

<?php

namespace app\controllers;
use \georgique\yii2\jsonrpc\Controller;

class JsonRpcController extends Controller {

	// Practically you don't need anything else in this controller, 
	// unless you want to customize entry point somehow.
	
}

以不同方式传递参数的入口点

<?php

namespace app\controllers;
use \georgique\yii2\jsonrpc\Controller;

class JsonRpcBodyController extends Controller {

	// With the customization JSON RPC params will be passed to the target action
	// as request body params, not as action function arguments
    public $paramsPassMethod = self::JSON_RPC_PARAMS_PASS_BODY;

}

具有目标操作的控制器,我们将要调用这些操作

<?php
namespace app\modules\api1\controllers;

// There are some minor side-effects of this solutions, because original request is made to the
// entry point, not to the target controller and action. Be careful working with Request object,
// especially when working on access restriction to the target actions. For example, you want an
// action to be reached only with GET verb only, but you do POST request to the endpoint. In that
// case you will get Internal Error because access will be denied.
class ExampleController extends \yii\web\Controller {

    // Note that URL patterns won't be used to resolve the method - this would not be resourse-wise.
    // Method string should simply be [[module.]controller.]action where module and controller parts
    // can be omitted, so default module and index controller will be used.
    public function actionTry() {
        return "You've got it!";
    }

    // Method params are directly translated into action arguments. Make sure your call matches action
    // signature.
    public function actionTryWithParams($foo) {
        return "Params received: \$foo = $foo.";
    }
    
    // Passing params as Yii request body params must be handy too, when we need to do a massive
    // attribute assignment for example.
    public function actionTryWithBodyParams() {
        $output = "Params received: ";
        $output_chunks = array();
        foreach (\Yii::$app->request->getBodyParams() as $name => $value) {
            $output_chunks[] = "$name = $value\n";
        }
        return $output . implode(', ', $output_chunks) . '.';
    }
 
}

现在,这是调用和响应的外观

-> {"jsonrpc": "2.0", "method": "api1.example.try", "id": 1}
<- {"jsonrpc": "2.0", "result": "You've got it!", "id": 1}

-> {"jsonrpc": "2.0", "method": "api1.example.try-with-params", "params": {"foo": "bar"}, "id": 2}
<- {"jsonrpc": "2.0", "result": "Params received: $foo = bar.", "id": 2}

// Using alternative entry point:
-> {"jsonrpc": "2.0", "method": "api1.example.try-with-body-params", "params": {"foo": "bar", "foo1": "bar1"}, "id": 2}
<- {"jsonrpc": "2.0", "result": "Params received: $foo = bar, $foo1 = bar1.", "id": 2}

-> {"jsonrpc": "2.0", "method": "api1.example.garbage", "id": 3}
<- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": 3}

-> [
	{"jsonrpc": "2.0", "method": "api1.example.try", "id": 1},
   	{"jsonrpc": "2.0", "method": "api1.example.try-with-params", "params": {"foo": "bar"}, "id": 2},
	{"jsonrpc": "2.0", "method": "api1.example.garbage", "id": 3}
   ]
<- [
	{"jsonrpc": "2.0", "result": "You've got it!", "id": 1},
   	{"jsonrpc": "2.0", "result": "Params received: $foo = bar.", "id": 2},
	{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": 3}
   ]

作者:George Shestayev george.shestayev@gmail.com