/json-rpc-laravel

JSON-RPC 2.0 for Laravel applications

Primary LanguagePHPMIT LicenseMIT

logo

JSON-RPC 2.0

Version PHP Version Build Status Coverage Downloads count License

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:

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.

ChangeLog

Release date Commits since latest release

Changes log can be found here.

Support

Issues Issues

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.