PHP code example of royalcms / laravel-jsonrpc-server

1. Go to this page and download the library: Download royalcms/laravel-jsonrpc-server library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

royalcms / laravel-jsonrpc-server example snippets




use AvtoDev\JsonRPC\RpcRouter;

RpcRouter::on('please_sum_array_values', 'YourNamespace\\SomeController@sum');
RpcRouter::on('show_full_request', 'YourNamespace\\SomeController@showInfo');



use Illuminate\Support\Facades\Route;

Route::post('/rpc', 'AvtoDev\\JsonRpc\\Http\\Controllers\\RpcController');



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(),
        ];
    }
}