avto-dev/json-rpc-laravel is a Laravel package for json rpc package.
It currently has 6 GitHub stars and 22.832 downloads on Packagist (latest version v2.7.0).
Install it with composer require avto-dev/json-rpc-laravel.
Discover more Laravel packages by avto-dev
or browse all Laravel packages to compare alternatives.
Last updated
JSON-RPC 2.0 is a remote procedure call protocol encoded in JSON. It is a very simple protocol, defining only a few data types and commands. JSON-RPC allows for notifications (data sent to the server that does not require a response) and for multiple calls to be sent to the server which may be answered out of order.
Require this package with composer using the following command:
$ composer require avto-dev/json-rpc-laravel "^2.0"
Installed
composeris required (how to install composer).
You need to fix the major version of package.
Register actions for your methods in ./routes/web.php using the facade RpcRouter:
<?php
use AvtoDev\JsonRPC\RpcRouter;
RpcRouter::on('please_sum_array_values', 'YourNamespace\\SomeController@sum');
RpcRouter::on('show_full_request', 'YourNamespace\\SomeController@showInfo');
This package already contains a simple controller implementation, which you can expand it or take it only as an example.
Add new route for RpcController:
<?php
use Illuminate\Support\Facades\Route;
Route::post('/rpc', 'AvtoDev\\JsonRpc\\Http\\Controllers\\RpcController');
Don't forget about to load RPC routes if you want to specify routes not in the file
./routes/web.php
Create a new controller containing procedures to be called by JSON request:
<?php
namespace YourNamespace;
use AvtoDev\JsonRpc\Requests\RequestInterface;
class SomeController
{
/**
* Get sum of array.
*
* @param RequestInterface $request
*
* @return int
*/
public function sum(RequestInterface $request): int
{
return (int) \array_sum($request->getParams());
}
/**
* Get info from request.
*
* @param RequestInterface $request
*
* @return mixed[]
*/
public function showInfo(RequestInterface $request): array
{
return [
'params' => $request->getParams(),
'notification' => $request->isNotification(),
'method_name' => $request->getMethod(),
'request ID' => $request->getId(),
];
}
}
$ curl 'http://localhost/rpc' \
-H 'Content-Type: application/json;charset=utf-8' \
--data '{"jsonrpc":"2.0","method":"please_sum_array_values","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}'
{
"jsonrpc": "2.0",
"result": 55,
"id": "UNIQ_REQUEST_ID"
}
$ curl 'http://localhost/rpc' \
-H 'Content-Type: application/json;charset=utf-8' \
--data '{"jsonrpc":"2.0","method":"show_full_request","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}'
{
"jsonrpc": "2.0",
"result": {
"params": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"notification": false,
"method_name": "please_sum_array_values",
"request ID": "UNIQ_REQUEST_ID"
},
"id": "UNIQ_REQUEST_ID"
}
$ curl 'http://localhost/rpc' \
-H 'Content-Type: application/json;charset=utf-8' \
--data '{"jsonrpc":"2.0","method":"undefined_method","id":"UNIQ_REQUEST_ID"}'
{
"jsonrpc": "2.0",
"error": {
"code": -32601,
"message": "Method not found"
},
"id": "UNIQ_REQUEST_ID"
}
When the Kernel@handle method is called, some events are fired:
Event class | Description
------------------------------- | -----------
ErroredRequestDetectedEvent | Detected not valid request from stack
RequestHandledEvent | Means the remote method was successfully called
RequestHandledExceptionEvent | An exception was thrown while executing the request
If necessary, you can create a Listener on this event and display received messages for debug.
The EventServiceProvider included with your Laravel application provides a convenient place to register all of your application's event listeners.
data.
Changes log can be found here.
If you will find any package errors, please, make an issue in current repository.
This is open-sourced software licensed under the MIT License.