Wiki source code of 1. Встановлення
Last modified by Ashterix on 2024/05/10 11:09
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
21.1 | 1 | {{box cssClass="floatinginfobox" width="400px" title="**Contents**"}} |
![]() |
1.1 | 2 | {{toc/}} |
3 | {{/box}} | ||
4 | |||
![]() |
21.1 | 5 | = Step 0: Setting up composer.json = |
![]() |
1.1 | 6 | |
![]() |
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}} |
![]() |
1.1 | 8 | |
![]() |
19.1 | 9 | {{code language="json" layout="LINENUMBERS" title="composer.json"}} |
![]() |
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 | |||
![]() |
6.2 | 22 | {{info}} |
![]() |
21.1 | 23 | Learn more about Symfony Flex in the [[Symfony documentation>>url:https://symfony.com/doc/current/setup/flex_private_recipes.html]] |
![]() |
6.2 | 24 | {{/info}} |
![]() |
1.1 | 25 | |
![]() |
7.2 | 26 | |
![]() |
21.1 | 27 | = Step 1: Installation = |
![]() |
1.1 | 28 | |
![]() |
21.1 | 29 | In your project directory's console, execute this command to download the latest version of this package: |
![]() |
1.1 | 30 | |
31 | {{code language="bash"}} | ||
![]() |
6.1 | 32 | composer require ufo-tech/json-rpc-bundle |
![]() |
1.1 | 33 | {{/code}} |
34 | |||
![]() |
6.2 | 35 | {{info}} |
![]() |
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]] |
![]() |
6.2 | 37 | {{/info}} |
![]() |
1.1 | 38 | |
![]() |
7.2 | 39 | |
40 | |||
![]() |
21.1 | 41 | = Step 2: Registering the Package = |
![]() |
7.2 | 42 | |
![]() |
21.1 | 43 | Ensure that the package is automatically registered in your project's {{code language="none"}}config/bundles.php{{/code}} file: |
![]() |
7.2 | 44 | |
![]() |
19.1 | 45 | {{code language="php" layout="LINENUMBERS" title="config/bundles.php"}} |
![]() |
12.1 | 46 | <?php |
47 | |||
48 | return [ | ||
![]() |
7.2 | 49 | // ... |
![]() |
12.1 | 50 | Ufo\JsonRpcBundle\UfoJsonRpcBundle::class => ['all' => true], |
51 | // ... | ||
![]() |
7.2 | 52 | ]; |
![]() |
21.1 | 53 | |
54 | |||
![]() |
7.2 | 55 | {{/code}} |
56 | |||
57 | |||
58 | |||
![]() |
21.1 | 59 | = Step 3: Adding Parameters = |
![]() |
7.2 | 60 | |
61 | {{warning}} | ||
![]() |
21.1 | 62 | **If you completed “Step 0,” this is configured automatically, and you can skip this step.** |
![]() |
7.2 | 63 | {{/warning}} |
64 | |||
![]() |
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: |
![]() |
7.2 | 66 | |
![]() |
19.1 | 67 | {{code language="yaml" layout="LINENUMBERS" title="config/packages/ufo_json_rpc.yaml"}} |
![]() |
7.3 | 68 | ufo_json_rpc: |
69 | security: | ||
![]() |
10.3 | 70 | protected_methods: ['POST'] # Protection of GET and POST requests |
![]() |
7.3 | 71 | token_key_in_header: 'Ufo-RPC-Token' # Name of the key in the header |
72 | clients_tokens: | ||
![]() |
10.3 | 73 | - 'ClientTokenExample' # Hardcoded token example. Importantly!!! Replace or delete it! |
74 | - '%env(resolve:UFO_API_TOKEN)%' # Token example from .env.local | ||
![]() |
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 | ||
![]() |
7.2 | 85 | |
![]() |
7.3 | 86 | {{/code}} |
![]() |
7.2 | 87 | |
![]() |
7.3 | 88 | (% class="wikigeneratedid" %) |
![]() |
21.1 | 89 | A detailed overview of configuration parameters is available in the [[Bundle Settings section>>doc:docs.JsonRpcBundle.config_v7.config]] |
![]() |
7.2 | 90 | |
![]() |
8.3 | 91 | |
92 | |||
![]() |
21.1 | 93 | = Step 4: Route Registration = |
![]() |
7.2 | 94 | |
![]() |
7.3 | 95 | {{warning}} |
![]() |
21.1 | 96 | **If you completed “Step 0,” this is configured automatically, and you can skip this step.** |
![]() |
7.3 | 97 | {{/warning}} |
![]() |
7.2 | 98 | |
![]() |
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: |
![]() |
7.2 | 100 | |
![]() |
19.1 | 101 | {{code language="yaml" layout="LINENUMBERS" title="config/routes/ufo_json_rpc.yaml"}} |
![]() |
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 | |||
![]() |
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: | ||
![]() |
7.3 | 110 | |
![]() |
19.1 | 111 | {{code language="yaml" layout="LINENUMBERS" title="config/routes/ufo_json_rpc.yaml"}} |
![]() |
10.2 | 112 | ufo_json_rpc_bundle: |
113 | resource: ../../vendor/ufo-tech/json-rpc-bundle/config/router.yaml | ||
![]() |
21.1 | 114 | prefix: /my_custom_api_route # specify an alternative path here |
![]() |
10.2 | 115 | trailing_slash_on_root: false |
116 | {{/code}} | ||
![]() |
7.3 | 117 | |
![]() |
21.1 | 118 | Now the API will be accessible at {{code language="none"}}https://example.com/my_custom_api_route{{/code}} |
![]() |
7.3 | 119 | |
![]() |
10.3 | 120 | |
![]() |
12.1 | 121 | |
![]() |
21.1 | 122 | = Step 5: Profit = |
![]() |
10.3 | 123 | |
![]() |
10.2 | 124 | {{success}} |
![]() |
21.1 | 125 | **Congratulations!!!** Your RPC server is ready to operate!!! |
![]() |
10.2 | 126 | {{/success}} |
![]() |
1.1 | 127 | |
![]() |
21.1 | 128 | = What Next? = |
![]() |
10.3 | 129 | |
![]() |
21.1 | 130 | == Does it really work? (c) == |
![]() |
10.3 | 131 | |
![]() |
21.1 | 132 | A GET request at the entry point will return documentation on the available methods and the parameters they accept. |
![]() |
10.3 | 133 | |
![]() |
14.1 | 134 | {{warning}} |
![]() |
21.1 | 135 | The format of the documentation may vary depending on your settings. See [[Bundle Settings>>doc:docs.JsonRpcBundle.config_v7.config]] |
![]() |
14.1 | 136 | {{/warning}} |
137 | |||
![]() |
21.1 | 138 | **{{code language="none"}}GET: /api{{/code}}** |
![]() |
10.3 | 139 | |
140 | {{code language="json"}} | ||
141 | { | ||
142 | "envelope": "JSON-RPC-2.0/UFO-RPC-6", | ||
143 | "contentType": "application/json", | ||
144 | "description": "", | ||
![]() |
20.2 | 145 | "transport": { |
146 | "sync": { | ||
147 | "scheme": "https", | ||
148 | "host": "example.com", | ||
149 | "path": "/api", | ||
150 | "method": "POST" | ||
151 | } | ||
152 | }, | ||
![]() |
10.3 | 153 | "methods": { |
![]() |
20.2 | 154 | "ping": { |
155 | "name": "ping", | ||
![]() |
10.3 | 156 | "description": "", |
157 | "parameters": [], | ||
158 | "returns": "string", | ||
159 | "responseFormat": "string" | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | {{/code}} | ||
164 | |||
![]() |
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. |
![]() |
10.3 | 166 | |
![]() |
21.1 | 167 | (% id="cke_bm_115746S" style="display:none" %)** **(%%)**{{code language="none"}}POST: /api{{/code}}** |
![]() |
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}} | ||
![]() |
12.1 | 186 | |
![]() |
21.1 | 187 | == Change [[Settings>>doc:docs.JsonRpcBundle.config_v7.config]] to meet your needs == |
![]() |
12.1 | 188 | |
189 | |||
![]() |
21.1 | 190 | == Add [[Custom Procedures>>doc:docs.JsonRpcBundle.add_rpc_service.WebHome]] == |