Wiki source code of 1. Встановлення

Last modified by Ashterix on 2024/05/10 11:09

Hide last authors
Ashterix 21.1 1 {{box cssClass="floatinginfobox" width="400px" title="**Contents**"}}
Ashterix 1.1 2 {{toc/}}
3 {{/box}}
4
Ashterix 21.1 5 = Step 0: Setting up composer.json =
Ashterix 1.1 6
Ashterix 21.1 7 To ensure that adding a package automatically configures your Symfony Flex, make the following changes to your {{code language="none"}}composer.json{{/code}}
Ashterix 1.1 8
Ashterix 19.1 9 {{code language="json" layout="LINENUMBERS" title="composer.json"}}
Ashterix 1.1 10 {
11 "extra" : {
12 "symfony": {
13 "endpoint": [
14 "https://api.github.com/repos/ufo-tech/recipes/contents/index.json?ref=main",
15 "flex://defaults"
16 ]
17 }
18 },
19 }
20 {{/code}}
21
Ashterix 6.2 22 {{info}}
Ashterix 21.1 23 Learn more about Symfony Flex in the [[Symfony documentation>>url:https://symfony.com/doc/current/setup/flex_private_recipes.html]]
Ashterix 6.2 24 {{/info}}
Ashterix 1.1 25
Ashterix 7.2 26
Ashterix 21.1 27 = Step 1: Installation =
Ashterix 1.1 28
Ashterix 21.1 29 In your project directory's console, execute this command to download the latest version of this package:
Ashterix 1.1 30
31 {{code language="bash"}}
Ashterix 6.1 32 composer require ufo-tech/json-rpc-bundle
Ashterix 1.1 33 {{/code}}
34
Ashterix 6.2 35 {{info}}
Ashterix 21.1 36 This command is relevant if you have installed Composer globally, as described in the [[Composer documentation>>https://getcomposer.org/doc/00-intro.md]]
Ashterix 6.2 37 {{/info}}
Ashterix 1.1 38
Ashterix 7.2 39
40
Ashterix 21.1 41 = Step 2: Registering the Package =
Ashterix 7.2 42
Ashterix 21.1 43 Ensure that the package is automatically registered in your project's {{code language="none"}}config/bundles.php{{/code}} file:
Ashterix 7.2 44
Ashterix 19.1 45 {{code language="php" layout="LINENUMBERS" title="config/bundles.php"}}
Ashterix 12.1 46 <?php
47
48 return [
Ashterix 7.2 49 // ...
Ashterix 12.1 50 Ufo\JsonRpcBundle\UfoJsonRpcBundle::class => ['all' => true],
51 // ...
Ashterix 7.2 52 ];
Ashterix 21.1 53
54
Ashterix 7.2 55 {{/code}}
56
57
58
Ashterix 21.1 59 = Step 3: Adding Parameters =
Ashterix 7.2 60
61 {{warning}}
Ashterix 21.1 62 **If you completed “Step 0,” this is configured automatically, and you can skip this step.**
Ashterix 7.2 63 {{/warning}}
64
Ashterix 21.1 65 For manual configuration of the bundle, add to the {{code language="none"}}config/packages{{/code}} directory a file named {{code language="none"}}ufo_json_rpc.yaml{{/code}} with the following content:
Ashterix 7.2 66
Ashterix 19.1 67 {{code language="yaml" layout="LINENUMBERS" title="config/packages/ufo_json_rpc.yaml"}}
Ashterix 7.3 68 ufo_json_rpc:
69 security:
Ashterix 10.3 70 protected_methods: ['POST'] # Protection of GET and POST requests
Ashterix 7.3 71 token_key_in_header: 'Ufo-RPC-Token' # Name of the key in the header
72 clients_tokens:
Ashterix 10.3 73 - 'ClientTokenExample' # Hardcoded token example. Importantly!!! Replace or delete it!
74 - '%env(resolve:UFO_API_TOKEN)%' # Token example from .env.local
Ashterix 7.3 75
76 # Configuration for API documentation
77 docs:
78 # Optional response details
79 response:
80 key_for_methods: services # Key used to map services for API methods
81 # Information about validations
82 validations:
83 json_schema: false # Indicates if JSON-schema is used for method validation
84 symfony_asserts: false # Indicates if an array of Symfony validation constraints is used
Ashterix 7.2 85
Ashterix 7.3 86 {{/code}}
Ashterix 7.2 87
Ashterix 7.3 88 (% class="wikigeneratedid" %)
Ashterix 21.1 89 A detailed overview of configuration parameters is available in the [[Bundle Settings section>>doc:docs.JsonRpcBundle.config_v7.config]]
Ashterix 7.2 90
Ashterix 8.3 91
92
Ashterix 21.1 93 = Step 4: Route Registration =
Ashterix 7.2 94
Ashterix 7.3 95 {{warning}}
Ashterix 21.1 96 **If you completed “Step 0,” this is configured automatically, and you can skip this step.**
Ashterix 7.3 97 {{/warning}}
Ashterix 7.2 98
Ashterix 21.1 99 For manual configuration of the bundle, add to the {{code language="none"}}config/routes{{/code}} directory a file named {{code language="none"}}ufo_json_rpc.yaml{{/code}} with the following content:
Ashterix 7.2 100
Ashterix 19.1 101 {{code language="yaml" layout="LINENUMBERS" title="config/routes/ufo_json_rpc.yaml"}}
Ashterix 7.3 102 ufo_json_rpc:
103 resource: ../../vendor/ufo-tech/json-rpc-bundle/config/router.yaml
104 prefix: /api
105 trailing_slash_on_root: false
106 {{/code}}
107
Ashterix 21.1 108 If left as is, the RPC API will be accessible at {{code language="none"}}/api{{/code}} e.g. {{code language="none"}}https://example.com/api{{/code}}
109 If you need to change the path, modify the route configuration as follows:
Ashterix 7.3 110
Ashterix 19.1 111 {{code language="yaml" layout="LINENUMBERS" title="config/routes/ufo_json_rpc.yaml"}}
Ashterix 10.2 112 ufo_json_rpc_bundle:
113 resource: ../../vendor/ufo-tech/json-rpc-bundle/config/router.yaml
Ashterix 21.1 114 prefix: /my_custom_api_route # specify an alternative path here
Ashterix 10.2 115 trailing_slash_on_root: false
116 {{/code}}
Ashterix 7.3 117
Ashterix 21.1 118 Now the API will be accessible at {{code language="none"}}https://example.com/my_custom_api_route{{/code}}
Ashterix 7.3 119
Ashterix 10.3 120
Ashterix 12.1 121
Ashterix 21.1 122 = Step 5: Profit =
Ashterix 10.3 123
Ashterix 10.2 124 {{success}}
Ashterix 21.1 125 **Congratulations!!!** Your RPC server is ready to operate!!!
Ashterix 10.2 126 {{/success}}
Ashterix 1.1 127
Ashterix 21.1 128 = What Next? =
Ashterix 10.3 129
Ashterix 21.1 130 == Does it really work? (c) ==
Ashterix 10.3 131
Ashterix 21.1 132 A GET request at the entry point will return documentation on the available methods and the parameters they accept.
Ashterix 10.3 133
Ashterix 14.1 134 {{warning}}
Ashterix 21.1 135 The format of the documentation may vary depending on your settings. See [[Bundle Settings>>doc:docs.JsonRpcBundle.config_v7.config]]
Ashterix 14.1 136 {{/warning}}
137
Ashterix 21.1 138 **{{code language="none"}}GET: /api{{/code}}**
Ashterix 10.3 139
140 {{code language="json"}}
141 {
142 "envelope": "JSON-RPC-2.0/UFO-RPC-6",
143 "contentType": "application/json",
144 "description": "",
Ashterix 20.2 145 "transport": {
146 "sync": {
147 "scheme": "https",
148 "host": "example.com",
149 "path": "/api",
150 "method": "POST"
151 }
152 },
Ashterix 10.3 153 "methods": {
Ashterix 20.2 154 "ping": {
155 "name": "ping",
Ashterix 10.3 156 "description": "",
157 "parameters": [],
158 "returns": "string",
159 "responseFormat": "string"
160 }
161 }
162 }
163 {{/code}}
164
Ashterix 21.1 165 The {{code language="none"}}ping{{/code}} method is set up immediately, you can execute a POST request right away to ensure that the server is operating.
Ashterix 10.3 166
Ashterix 21.1 167 (% id="cke_bm_115746S" style="display:none" %)** **(%%)**{{code language="none"}}POST: /api{{/code}}**
Ashterix 10.3 168
169 **Request**:
170
171 {{code language="json"}}
172 {
173 "id": "some_request_id",
174 "method": "ping"
175 }
176 {{/code}}
177
178 **Response**:
179
180 {{code language="json"}}
181 {
182 "id": "some_request_id",
183 "result": "PONG"
184 }
185 {{/code}}
Ashterix 12.1 186
Ashterix 21.1 187 == Change [[Settings>>doc:docs.JsonRpcBundle.config_v7.config]] to meet your needs ==
Ashterix 12.1 188
189
Ashterix 21.1 190 == Add [[Custom Procedures>>doc:docs.JsonRpcBundle.add_rpc_service.WebHome]] ==