2017 © Pedro Peláez
 

yii2-extension yii2-jsonrpc

Yii2 JSON-RPC server implementation

image

georgique/yii2-jsonrpc

Yii2 JSON-RPC server implementation

  • Wednesday, June 13, 2018
  • by georgique
  • Repository
  • 1 Watchers
  • 4 Stars
  • 35 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 9 Versions
  • 483 % Grown

The README.md

Yii2 JSON-RPC

Yii2 extension for JSON-RPC server implementation. Works not as Remote Procedure Call, but Remote Action Call, therefor leaves all Yii2 power for your service., (*1)

Build Status, (*2)

Features

  • Uses full Yii2 power, because method string is translated into a route.
  • Lightweight.
  • Fully JSON-RPC 2.0 compliant.

Usage

Entry point:, (*3)

<?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.

}

Entry point with different way to pass params:, (*4)

<?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;

}

Controller with target actions which we are going to call:, (*5)

<?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) . '.';
    }

}

Now this is how calls and responses will look like:, (*6)

-> {"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}
   ]

Author: George Shestayev george.shestayev@gmail.com, (*7)

The Versions

13/06 2018

dev-master

9999999-dev

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc

13/06 2018

1.0.1

1.0.1.0

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc

04/06 2018

dev-develop

dev-develop

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc

22/05 2018

1.0.0

1.0.0.0

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc

07/03 2018

1.0-beta.2

1.0.0.0-beta2

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc

27/02 2018

1.0-beta.1

1.0.0.0-beta1

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc

26/02 2018

1.0-alpha.3

1.0.0.0-alpha3

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc

26/02 2018

1.0-alpha.2

1.0.0.0-alpha2

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc

09/01 2018

1.0-alpha.1

1.0.0.0-alpha1

Yii2 JSON-RPC server implementation

  Sources   Download

by George Shestayev

yii2 bootstrap json-rpc