Download the PHP package rikudou/json-rpc-bundle without Composer
On this page you can find all versions of the php package rikudou/json-rpc-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rikudou/json-rpc-bundle
More information about rikudou/json-rpc-bundle
Files in rikudou/json-rpc-bundle
Package json-rpc-bundle
Short Description JSON-RPC bundle for Symfony using modern php
License MIT
Informations about the package json-rpc-bundle
JSON-RPC bundle for Symfony.
This allows you to respond and handle JSON-RPC requests using modern php.
Both batch requests and single requests are supported.
Installation
Requires php 8 and Symfony 5.x.
composer require rikudou/json-rpc-bundle
Usage
Create a controller that will be handling the traffic, the controller is very simple and basically looks like this:
The JsonRpcRequest
object is automatically injected based on current request and the JsonRpcResponder
takes care
of locating the correct method and providing response.
Creating methods
Creating methods is very simple and you have 3 options:
- use the
Rikudou\JsonRpcBundle\Attribute\JsonRpcMethod
attribute in a callable object (implementing__invoke()
) - implement the
\Rikudou\JsonRpcBundle\JsonRpc\JsonRpcMethod
interface - registering callables manually
Note that while the attribute and interface have the same name, the namespace is different.
Example with attribute
Or if you want to accept parameters:
If any other parameters are provided, an exception might be thrown, to avoid that you can add a variadic parameter that catches all other parameters (the name of the parameter doesn't matter):
Example with interface
You can also check for parameters:
Registering other callables
You can register other callables manually in a configuration file (for example config/packages/json_rpc.yaml
).
Here's an example configuration:
These formats of callables are supported:
- functions
globalFunction
- static methods
App\MyClass::someMethod
['App\MyClass', 'someMethod']
- service method
['@App\MyService', 'someMethod']
Note that the service name starts with @
.
Working with request object
If you want to do some checks on the request object you can do so before passing it to the JsonRpcResponder
service.
The JsonRpcRequest
is a marker interface that doesn't contain any methods meaning you need to check yourself if it's
an instance of JsonRpcSingleRequest
or JsonRpcBatchRequest
.
The JsonRpcSingleRequest
object contains these methods:
getMethod(): string
getParams(): ?array
getId(): int|string|null
The JsonRpcBatchRequest
contains these methods:
getRequests(): iterable<JsonRpcSingleRequest>
- returns list of individual requests
In your controller you can also typehint the concrete class (JsonRpcSingleRequest
or JsonRpcBatchRequest
) but
in that case you will get a TypeError
when the other type of request arrives.
If you want to get the current request outside of a controller class, you will need to use the service
JsonRpcRequestParser
:
Working with response object
If you want to alter the response in your controller, you can call JsonRpcResponse
's getJson()
method which returns
an array with the raw data that would be encoded to JSON.
You can check whether the response is a single one or batch one by checking for instance of JsonRpcSingleResponse
and JsonRpcBatchResponse
.