1. Go to this page and download the library: Download larananas/lumen-json-rpc 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/ */
larananas / lumen-json-rpc example snippets
declare(strict_types=1);
;
use Lumen\JsonRpc\Server\JsonRpcServer;
$config = new Config([
'handlers' => [
'paths' => [__DIR__ . '/../handlers'],
'namespace' => 'App\\Handlers\\',
],
]);
$server = new JsonRpcServer($config);
$server->run();
declare(strict_types=1);
namespace App\Handlers;
use Lumen\JsonRpc\Support\RequestContext;
final class User
{
public function get(RequestContext $context, int $id): array
{
return [
'id' => $id,
'name' => 'Example User',
'requested_by' => $context->authUserId,
];
}
}
use Lumen\JsonRpc\Dispatcher\HandlerFactoryInterface;
use Lumen\JsonRpc\Support\RequestContext;
$factory = new class($db) implements HandlerFactoryInterface {
public function __construct(private DatabaseService $db) {}
public function create(string $className, RequestContext $context): object
{
return new $className($this->db);
}
};
$server->setHandlerFactory($factory);
use Lumen\JsonRpc\Middleware\MiddlewareInterface;
use Lumen\JsonRpc\Protocol\Request;
use Lumen\JsonRpc\Protocol\Response;
use Lumen\JsonRpc\Support\RequestContext;
$server->addMiddleware(new class implements MiddlewareInterface {
public function process(Request $request, RequestContext $context, callable $next): ?Response
{
error_log("[JSON-RPC] -> {$request->method}");
$response = $next($request, $context);
error_log('[JSON-RPC] <- done');
return $response;
}
});
use Lumen\JsonRpc\Dispatcher\ProcedureDescriptor;
$registry = $server->getRegistry();
$registry->register('math.add', MathHandler::class, 'add', [
'description' => 'Add two numbers',
]);
// Or batch-register descriptor objects:
$registry->registerDescriptors([
new ProcedureDescriptor('math.add', MathHandler::class, 'add', ['description' => 'Add two numbers']),
new ProcedureDescriptor('math.multiply', MathHandler::class, 'multiply'),
]);
use Lumen\JsonRpc\Validation\RpcSchemaProviderInterface;
final class Product implements RpcSchemaProviderInterface
{
public static function rpcValidationSchemas(): array
{
return [
'create' => [
'type' => 'object',
'