avto-dev / json-rpc-laravel
Json Rpc package
Installs: 18 270
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 3
Forks: 8
Open Issues: 0
Requires
- php: ^8.1
- ext-json: *
- illuminate/contracts: ~10.0 || ~11.0
- illuminate/http: ~10.0 || ~11.0
- illuminate/routing: ~10.0 || ~11.0
- illuminate/support: ~10.0 || ~11.0
Requires (Dev)
- laravel/laravel: ~10.0 || ~11.0
- mockery/mockery: ^1.6.5
- phpstan/phpstan: ^1.10.66
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2024-12-01 00:07:31 UTC
README
JSON-RPC 2.0
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.
Install
Require this package with composer using the following command:
$ composer require avto-dev/json-rpc-laravel "^2.0"
Installed
composer
is required (how to install composer).
You need to fix the major version of package.
Usage example
Create routes
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 RPC controller
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(), ]; } }
Send RPC-request
$ 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" }
Events
When the Kernel@handle
method is called, some events are fired:
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.
ChangeLog
Changes log can be found here.
Support
If you will find any package errors, please, make an issue in current repository.
License
This is open-sourced software licensed under the MIT License.