1. Installation
Step 0: Setting up composer.json
To ensure that adding a package automatically configures your Symfony Flex, make the following changes to your composer.json
2
3
4
5
6
7
8
9
10
"extra" : {
"symfony": {
"endpoint": [
"https://api.github.com/repos/ufo-tech/recipes/contents/index.json?ref=main",
"flex://defaults"
]
}
},
}
Step 1: Installation
In your project directory's console, execute this command to download the latest version of this package:
Step 2: Registering the Package
Ensure that the package is automatically registered in your project's config/bundles.php file:
2
3
4
5
6
7
8
9
return [
// ...
Ufo\JsonRpcBundle\UfoJsonRpcBundle::class => ['all' => true],
// ...
];
Step 3: Adding Parameters
For manual configuration of the bundle, add to the config/packages directory a file named ufo_json_rpc.yaml with the following content:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
security:
protected_methods: ['POST'] # Protection of GET and POST requests
token_key_in_header: 'Ufo-RPC-Token' # Name of the key in the header
clients_tokens:
- 'ClientTokenExample' # Hardcoded token example. Importantly!!! Replace or delete it!
- '%env(resolve:UFO_API_TOKEN)%' # Token example from .env.local
# Configuration for API documentation
docs:
# Optional response details
response:
key_for_methods: services # Key used to map services for API methods
# Information about validations
validations:
json_schema: false # Indicates if JSON-schema is used for method validation
symfony_asserts: false # Indicates if an array of Symfony validation constraints is used
A detailed overview of configuration parameters is available in the Bundle Settings section
Step 4: Route Registration
For manual configuration of the bundle, add to the config/routes directory a file named ufo_json_rpc.yaml with the following content:
2
3
4
resource: ../../vendor/ufo-tech/json-rpc-bundle/config/router.yaml
prefix: /api
trailing_slash_on_root: false
If left as is, the RPC API will be accessible at /api e.g. https://example.com/api
If you need to change the path, modify the route configuration as follows:
2
3
4
resource: ../../vendor/ufo-tech/json-rpc-bundle/config/router.yaml
prefix: /my_custom_api_route # specify an alternative path here
trailing_slash_on_root: false
Now the API will be accessible at https://example.com/my_custom_api_route
Step 5: Profit
What Next?
Does it really work? (c)
A GET request at the entry point will return documentation on the available methods and the parameters they accept.
GET: /api
"envelope": "JSON-RPC-2.0/UFO-RPC-6",
"contentType": "application/json",
"description": "",
"transport": {
"sync": {
"scheme": "https",
"host": "example.com",
"path": "/api",
"method": "POST"
}
},
"methods": {
"ping": {
"name": "ping",
"description": "",
"parameters": [],
"returns": "string",
"responseFormat": "string"
}
}
}
The ping method is set up immediately, you can execute a POST request right away to ensure that the server is operating.
POST: /api
Request:
"id": "some_request_id",
"method": "ping"
}
Response:
"id": "some_request_id",
"result": "PONG"
}