Last modified by Ashterix on 2024/05/16 12:35

From version 3.10
edited by Ashterix
on 2024/05/16 12:35
Change comment: There is no comment for this version
To version 1.1
edited by Ashterix
on 2024/05/10 11:08
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,47 +1,46 @@
1 -{{box cssClass="floatinginfobox" title="**Content**"}}
1 +{{box cssClass="floatinginfobox" width="400px" title="**Contents**"}}
2 2  {{toc/}}
3 3  {{/box}}
4 4  
5 5  You can easily add your own API methods to the RPC server.
6 6  
7 -To do this, you just need to create any class that will become an API Service anywhere in your application. This class must implement the interface {{code language="none"}}Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService{{/code}}. This interface does not impose any logic on the class; it is only necessary for Symfony to understand that it should treat it as a service available to the RPC server.
7 +To do this, simply create any class in your application that is to become an API Service; this class must implement the interface {{code language="none"}}Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService{{/code}}. This interface does not impose any logic on the class; it is only necessary for Symfony to understand that it should be treated as a service available to the RPC server.
8 8  
9 -Implement any public method in your class, and it will automatically be available as an API Service.
9 +Implement any public method in your class and it will automatically be available as an API Service.
10 10  
11 -(% id="cke_bm_318406S" style="display:none" %) (%%) After following the previous instructions, new methods will already be available in the API. By default, method names are composed of {{code language="none"}}<className>.<methodName>{{/code}}.
11 +(% id="cke_bm_318406S" style="display:none" %) (%%) After following the previous instructions, you will have new methods available in the API. By default, method names consist of {{code language="none"}}<className>.<methodName>{{/code}}.
12 12  
13 13  = Naming Classes =
14 14  
15 -If necessary, you can create several classes that will be available as API Services.
16 -Considering their naming {{code language="none"}}<className>.<methodName>{{/code}}, I recommend treating the method name as a simple indication of what it does and the class name as the namespace of the API method.
15 +If needed, you can create multiple classes that will be available as API Services.
16 +Considering their naming **<className>.<methodName>**, I recommend approaching the choice of method name as a simple indication of what it does, and the class name as the namespace of the API method.
17 17  
18 -(% class="box successmessage" %)
19 -(((
20 -== **Best Practices** ==
18 +{{success}}
19 +== **Best Practice** ==
21 21  
22 -The class **//Messenger//** contains methods **//sendEmail()//**, **sendSms()**; the class **CartResolver** contains methods **//addProduct()//**, **getProducts()**, **calculateDiscount()**.
21 +The **//Messenger//** class contains methods **//sendEmail()//**, **sendSms()**; the **CartResolver** class contains methods **//addProduct()//**, **getProducts()**, **calculateDiscount()**.
23 23  
24 24  In the API, the following methods will be available:
25 25  
26 -* **CartResolver.addProduct**
27 -* **CartResolver.getProducts**
28 -* **CartResolver.calculateDiscount**
29 -* **Messenger.sendEmail**
30 -* **Messenger.sendSms**
25 +* **//CartResolver.addProduct//**
26 +* **//CartResolver.getProducts//**
27 +* **//CartResolver.calculateDiscount//**
28 +* **//Messenger.sendEmail//**
29 +* **//Messenger.sendSms//**
31 31  
32 -Even just by looking at the method names, it is clear what your server can do.
31 +Just by looking at the method names, it's clear what your server can do.
33 33  
34 -This approach allows you to use the same method names in different classes:
33 +This approach allows for the use of identical method names in different classes:
35 35  
36 -* **ProductService.create**
37 -* **ProductService.getName**
38 -* **UserService.create**
39 -* **UserService.getName**
40 -)))
35 +* **//ProductService.create//**
36 +* **//ProductService.getName//**
37 +* **//UserService.create//**
38 +* **//UserService.getName//**
39 +{{/success}}
41 41  
42 -= Simple Start =
41 += Getting Started =
43 43  
44 -The following example should demonstrate the ease of adding your methods to the API.
43 +The following example demonstrates how easy it is to add your methods to the API.
45 45  
46 46  (% class="row" %)
47 47  (((
... ... @@ -58,28 +58,30 @@
58 58   public function __construct(
59 59   // connecting some dependencies to retrieve data
60 60   ) {}
61 -
60 +
62 62   public function getUserNameByUuid(
63 63   string $userId
64 - ): string {
65 - // some logic to get user info by id
63 + ): string
64 + {
65 + // some logic get user info by id
66 66   return 'some result';
67 67   }
68 -
68 +
69 69   public function sendEmail(
70 - string $email,
71 - string $text,
70 + string $email,
71 + string $text,
72 72   string $subject = 'Message without subject'
73 - ): bool {
74 - // some logic to send email
73 + ): bool
74 + {
75 + // some logic send email
75 75   return true;
76 76   }
77 77  }
78 78  {{/code}}
79 79  
80 -To the right, you see an example of the documentation that the server will generate for this class.
81 +To the right is an example of the documentation that the server will generate for this class.
81 81  
82 -The main information about parameter names, their types, and optionality is obtained thanks to (% class="box code" %)ReflectionClass(%%), with the important points being the order of method arguments and their type hints.
83 +Basic information regarding parameter names, their types, and optionality is obtained thanks to {{code language="none"}}ReflectionClass{{/code}}, with the order of method arguments, their typehints being crucial.
83 83  )))
84 84  
85 85  (% class="col-xs-12 col-sm-6" %)
... ... @@ -86,47 +86,47 @@
86 86  (((
87 87  {{code language="json" layout="LINENUMBERS" title="== Documentation =="}}
88 88  {
89 - "methods": {
90 - "ExampleApi.getUserNameByUuid": {
91 - "name": "ExampleApi.getUserNameByUuid",
92 - "description": "",
93 - "parameters": {
94 - "userId": {
95 - "type": "string",
96 - "name": "userId",
97 - "description": "",
98 - "optional": false
90 + "methods": {
91 + "ExampleApi.getUserNameByUuid": {
92 + "name": "ExampleApi.getUserNameByUuid",
93 + "description": "",
94 + "parameters": {
95 + "userId": {
96 + "type": "string",
97 + "name": "userId",
98 + "description": "",
99 + "optional": false
99 99   }
100 100   },
101 - "returns": "string",
102 - "responseFormat": "string"
102 + "returns": "string",
103 + "responseFormat": "string"
103 103   },
104 - "ExampleApi.sendEmail": {
105 - "name": "ExampleApi.sendEmail",
106 - "description": "",
107 - "parameters": {
108 - "email": {
109 - "type": "string",
110 - "name": "email",
111 - "description": "",
112 - "optional": false
105 + "ExampleApi.sendEmail": {
106 + "name": "ExampleApi.sendEmail",
107 + "description": "",
108 + "parameters": {
109 + "email": {
110 + "type": "string",
111 + "name": "email",
112 + "description": "",
113 + "optional": false
113 113   },
114 - "text": {
115 - "type": "string",
116 - "name": "text",
117 - "description": "",
118 - "optional": false
115 + "text": {
116 + "type": "string",
117 + "name": "text",
118 + "description": "",
119 + "optional": false
119 119   },
120 - "subject": {
121 - "type": "string",
122 - "name": "subject",
123 - "description": "",
124 - "optional": true,
125 - "default": "Message without subject"
121 + "subject": {
122 + "type": "string",
123 + "name": "subject",
124 + "description": "",
125 + "optional": true,
126 + "default": "Message without subject"
126 126   }
127 127   },
128 - "returns": "boolean",
129 - "responseFormat": "boolean"
129 + "returns": "boolean",
130 + "responseFormat": "boolean"
130 130   }
131 131   }
132 132  }
... ... @@ -144,10 +144,10 @@
144 144  (((
145 145  {{code language="json" layout="LINENUMBERS" title="Request"}}
146 146  {
147 - "id": "example_request",
148 - "method": "ExampleApi.getUserNameByUuid",
149 - "params": {
150 - "userId": "1111"
148 + "id": "example_request",
149 + "method": "ExampleApi.getUserNameByUuid",
150 + "params": {
151 + "userId": "1111"
151 151   }
152 152  }
153 153  {{/code}}
... ... @@ -157,9 +157,9 @@
157 157  (((
158 158  {{code language="json" layout="LINENUMBERS" title="Response"}}
159 159  {
160 - "id": "example_request",
161 - "result": "some result",
162 - "jsonrpc": "2.0"
161 + "id": "example_request",
162 + "result": "some result",
163 + "jsonrpc": "2.0"
163 163  }
164 164  {{/code}}
165 165  )))
... ... @@ -171,12 +171,12 @@
171 171  (((
172 172  {{code language="json" layout="LINENUMBERS" title="Request"}}
173 173  {
174 - "id": "example_request",
175 - "method": "ExampleApi.sendEmail",
176 - "params": {
177 - "email": "user@example.com",
178 - "subject": "This is a test mail",
179 - "text": "Hi! This is a test email sent by the API"
175 + "id": "example_request",
176 + "method": "ExampleApi.sendEmail",
177 + "params": {
178 + "email": "user@example.com",
179 + "subject": "This is test mail",
180 + "text": "Hi! This is test send mail by API"
180 180   }
181 181  }
182 182  {{/code}}
... ... @@ -186,9 +186,9 @@
186 186  (((
187 187  {{code language="json" layout="LINENUMBERS" title="Response"}}
188 188  {
189 - "id": "example_request",
190 - "result": true,
191 - "jsonrpc": "2.0"
190 + "id": "example_request",
191 + "result": true,
192 + "jsonrpc": "2.0"
192 192  }
193 193  {{/code}}
194 194  )))
... ... @@ -196,14 +196,14 @@
196 196  
197 197  (% class="box warningmessage" %)
198 198  (((
199 -To simplify the documentation, all further examples will be provided using a single method (//**sendEmail**//).
200 +To simplify the understanding of the documentation, all subsequent examples will be provided using a single method (//**sendEmail**//).
200 200  )))
201 201  
202 -= Description of Methods and Parameters =
203 += Method and Parameter Descriptions =
203 203  
204 -The documenter relies on all available data belonging to the method and its arguments (names, input and output data types, docblocks). Thus, the description of methods and parameters can be enriched using docblocks.
205 +The documenter relies on all available data related to the method and its arguments (names, types of input and output data, docblocks). Thus, the descriptions of methods and parameters can be enriched through docblocks.
205 205  
206 -Let's add a description for the method and its parameters.
207 +Let's add a description of the method and parameters.
207 207  
208 208  (% class="row" %)
209 209  (((
... ... @@ -212,6 +212,7 @@
212 212  {{code language="php" layout="LINENUMBERS" title="== Code =="}}
213 213  <?php
214 214  // ...
216 +
215 215   /**
216 216   * A method for sending an email message
217 217   * @param string $email The email address
... ... @@ -220,17 +220,21 @@
220 220   * @return bool
221 221   */
222 222   public function sendEmail(
223 - string $email,
224 - string $text,
225 + string $email,
226 + string $text,
225 225   string $subject = 'Message without subject'
226 - ): bool {
227 - // some logic to send email
228 + ): bool
229 + {
230 + // some logic send email
228 228   return true;
229 229   }
233 +
230 230  // ...
231 231  {{/code}}
232 232  
233 233  Note the documentation now displays additional information about the method and its parameters.
238 +
239 +
234 234  )))
235 235  
236 236  (% class="col-xs-12 col-sm-6" %)
... ... @@ -237,33 +237,33 @@
237 237  (((
238 238  {{code language="json" layout="LINENUMBERS" title="== Documentation =="}}
239 239  {
240 - "methods": {
241 - "ExampleApi.sendEmail": {
242 - "name": "ExampleApi.sendEmail",
243 - "description": "A method for sending an email message",
244 - "parameters": {
245 - "email": {
246 - "type": "string",
247 - "name": "email",
248 - "description": "The email address",
249 - "optional": false
246 + "methods": {
247 + "ExampleApi.sendEmail": {
248 + "name": "ExampleApi.sendEmail",
249 + "description": "A method for sending an email message",
250 + "parameters": {
251 + "email": {
252 + "type": "string",
253 + "name": "email",
254 + "description": "The email address",
255 + "optional": false
250 250   },
251 - "text": {
252 - "type": "string",
253 - "name": "text",
254 - "description": "Message body",
255 - "optional": false
257 + "text": {
258 + "type": "string",
259 + "name": "text",
260 + "description": "Message body",
261 + "optional": false
256 256   },
257 - "subject": {
258 - "type": "string",
259 - "name": "subject",
260 - "description": "Optional message subject parameter",
261 - "optional": true,
262 - "default": "Message without subject"
263 + "subject": {
264 + "type": "string",
265 + "name": "subject",
266 + "description": "Optional message subject parameter",
267 + "optional": true,
268 + "default": "Message without subject"
263 263   }
264 264   },
265 - "returns": "boolean",
266 - "responseFormat": "boolean"
271 + "returns": "boolean",
272 + "responseFormat": "boolean"
267 267   }
268 268   }
269 269  }
... ... @@ -271,17 +271,18 @@
271 271  )))
272 272  )))
273 273  
274 -= RPC Attributes =
280 += RPC attributes =
275 275  
276 -For more flexible customization of your API methods, JsonRpcBundle uses [[PHP attributes>>url:https://www.php.net/manual/en/language.attributes.overview.php]].
282 +For more flexible configuration of your API methods, JsonRpcBundle uses such a tool as [[php attributes>>https://www.php.net/manual/en/language.attributes.overview.php]].
277 277  
278 -With the help of specialized attributes, you can:
284 +With specialized attributes, you can:
279 279  
280 -* set aliases for methods;
281 -* specify the response format for methods (if the response is in the form of an array, object, or collection of objects);
282 -* configure input parameter validation;
283 -* set up response caching.
286 +* Set aliases for methods;
287 +* Specify the response format for methods (if the response is in the form of an array, an object, or a collection of objects);
288 +* Set up validation of input parameters;
289 +* Configure caching of responses.
284 284  
285 -Read more about these attributes:
291 +Learn more about these attributes:
286 286  
287 287  {{children/}}
294 +