maoxuner/laravel-jsonrpc

JSON-RPC 2.0 Server/Client For Laravel

v1.3.0 2025-07-24 05:10 UTC

This package is auto-updated.

Last update: 2025-07-24 05:11:52 UTC


README

Entrypoint

Add route for jsonrpc entrypoint

\Illuminate\Support\Facades\Route::post('/jsonrpc', 'maoxuner\LaravelJsonRpc\Server\Server@entrypoint');

Configure

php artisan vendor:publish --tags=jsonrpc

Do some modification such as changing namespace to App\\Actions

Create a procedure

Create a procedure class under the namespace configured before with a public function handle.

<?php

namespace App\Actions;

class Ping
{
    public function handle(): string
    {
        return 'Pong';
    }
}
<?php

namespace App\Actions;

class Say
{
    public function handle(string $name, string $foo = 'foo', ?string $bar = null): array
    {
        return [
            'message' => 'Hello, '.$name.'!',
            'foo' => $foo,
            'bar' => $bar,
        ];
    }
}

Test Client

Use tinker create a client and then call Ping procedure. Params are supported by passing param position/name indexed array as second argument to execute.

php artisan tinker
$client = new \maoxuner\LaravelJsonRpc\Client\Client(\Illuminate\Support\Facades\Http::baseUrl('http://foo-bar/jsonrpc'));
echo $client->execute('Ping');
dump($client->execute('Say', ['World', 'oof']));
dump($client->execute('Say', ['bar' => 'rab', 'name' => 'World']));