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