alex-kudrya / laravel-jsonrpc
JSON RPC 2.0 for Laravel
Installs: 11
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Forks: 0
pkg:composer/alex-kudrya/laravel-jsonrpc
Requires
- php: >= 8.2
- illuminate/contracts: >= 10
- illuminate/http: >= 10
- illuminate/support: >= 10
- illuminate/validation: >= 10
- laravel/framework: >= 10
README
What is JSON-RPC you can learn here - #JSONRPC
Installation
composer require alex-kudrya/laravel-jsonrpc
In app.php add to providers array \AlexKudrya\Adminix\Providers\AdminixServiceProvider::class
// config/app.php
'providers' => [
...
\AlexKudrya\LaravelJsonRpc\Providers\JsonRpcServiceProvider::class,
...
]
Publish package to your app
php artisan vendor:publish --provider=AlexKudrya\LaravelJsonRpc\Providers\JsonRpcServiceProvider
How it works
JSON-RPC API it is an Api with single endpoint, by default it is https://your-domain.com/json-rpc/v1, json-rpc/v1 part can be configured in config/json_rpc.php.
Requests uses always only POST method.
There are exist fixed request - response structure for this protocol.
Example
Request
{
"jsonrpc": "2.0",
"method": "ControllerName@MethodName",
"params": {
"parameter": "value"
},
"id": "some_unique_request_id"
}
Responce
{
"jsonrpc": "2.0",
"result": {
"key": "value"
},
"id": "some_unique_request_id"
}
jsonrpc, method, params, id are required fields for JSON-RPC request
| Field | Description |
|---|---|
| jsonrpc | Just always equal to 2.0, it is constant, dont think about it. |
| method | The method we calling to. |
| params | JSON with input data, can be empty, but must be exist. |
| id | An unique request ID, need for front-end. When you use batch-requests, your front-end need to indentify which responce for which request was returned |
Calling Method consist from these parts:
- Controller name
- Separator
- Method name
Example: User@get\
In this example:
- "User" is a controller name, means
App\Http\Controllers\JsonRpc\UserControllercontroller/class. App\Http\Controllers\JsonRpc it is acontrollers_root_namespacefromconfig/json_rpc.phpfile, and Controller is acontrollers_postfixfromconfig/json_rpc.phpfile. - "@" is a separator,
controllers_method_delimiterfromconfig/json_rpc.phpfile. - "get" means we calling get() method from this controller.
As a result, method User@get calling get() method from App\Http\Controllers\JsonRpc\UserController
Check Server
To check server status you need make this request:
{
"jsonrpc": "2.0",
"method": "ping",
"params": {},
"id": "dlalkmd1231da"
}
If everythig is ok you will receive this responce:
{
"jsonrpc": "2.0",
"result": {
"message": "pong"
},
"id": "dlalkmd1231da"
}
Basic usage
Request:
{
"jsonrpc": "2.0",
"method": "Product@create",
"params": {
"name": "pencil",
"price": "1.5"
},
"id": "lsfkkj3fl3k92202jf2n32k430f"
}
Controller:
// app/Http/Controllers/JsonRpc/ProductController.php
namespace App\Http\Controllers\JsonRpc;
use AlexKudrya\LaravelJsonRpc\Requests\JsonRpcRequest;
use App\Http\Controllers\Controller;
class ProductController extends Controller
{
public function create(JsonRpcRequest $request)
{
$request->params(); // ['name' => 'pencil', 'price' => '1.5']
$request->params('name'); // pencil
$request->param('price'); // 1.5
$request->id(); // lsfkkj3fl3k92202jf2n32k430f
$request->method() // Product@create
$request->method(true) // ["controller" => "Product", "method" => "create"]
// you can use your input params like this:
$product = Product::create([
'name' => $request->params('name'),
'price' => $request->params('price'),
])
// or like this:
$product = Product::create($request->params())
return $product;
}
}
Responce:
{
"jsonrpc": "2.0",
"result": {
"id": "1",
"name": "pencil",
"price": "1.5"
},
"id": "lsfkkj3fl3k92202jf2n32k430f"
}
Atiention
- To use request validation your Request class must extends
AlexKudrya\LaravelJsonRpc\Requests\JsonRpcRequestclass. - Do not use
$request->all()or$request->input()to get your input parameters, you need to use$request->params()or$request->param({key})for this purpose
Batch requests
If you do not need to make several requests in parallel, you can use batch requests. The peculiarity of this type of request is that requests are executed sequentially in the order in which they are located in the request. Keep in mind that the failure of one request does not block the execution of the following requests.
You can use batch requests to send several requests in one:
Request:
[
{
"jsonrpc": "2.0",
"method": "Product@create",
"params": {
"name": "pencil",
"price": "1.5"
},
"id": "lsfkkj3fl3k92202jf2n32k430f"
},
{
"jsonrpc": "2.0",
"method": "Product@create",
"params": {
"name": "pen",
"price": "3.2"
},
"id": "dqsfkfOd2dwkdwo1231dlslawm20"
}
]
Responce:
[
{
"jsonrpc": "2.0",
"result": {
"id": "1",
"name": "pencil",
"price": "1.5"
},
"id": "lsfkkj3fl3k92202jf2n32k430f"
},
{
"jsonrpc": "2.0",
"result": {
"id": "2",
"name": "pen",
"price": "3.2"
},
"id": "dqsfkfOd2dwkdwo1231dlslawm20"
}
]