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

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

Summary

Details

Page properties
Content
... ... @@ -1,46 +1,47 @@
1 -{{box cssClass="floatinginfobox" width="400px" title="**Contents**"}}
1 +{{box cssClass="floatinginfobox" title="**Content**"}}
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, 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.
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.
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, you will have new methods available in the API. By default, method names consist of {{code language="none"}}<className>.<methodName>{{/code}}.
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}}.
12 12  
13 13  = Naming Classes =
14 14  
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.
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.
17 17  
18 -{{success}}
19 -== **Best Practice** ==
18 +(% class="box successmessage" %)
19 +(((
20 +== **Best Practices** ==
20 20  
21 -The **//Messenger//** class contains methods **//sendEmail()//**, **sendSms()**; the **CartResolver** class contains methods **//addProduct()//**, **getProducts()**, **calculateDiscount()**.
22 +The class **//Messenger//** contains methods **//sendEmail()//**, **sendSms()**; the class **CartResolver** contains methods **//addProduct()//**, **getProducts()**, **calculateDiscount()**.
22 22  
23 23  In the API, the following methods will be available:
24 24  
25 -* **//CartResolver.addProduct//**
26 -* **//CartResolver.getProducts//**
27 -* **//CartResolver.calculateDiscount//**
28 -* **//Messenger.sendEmail//**
29 -* **//Messenger.sendSms//**
26 +* **CartResolver.addProduct**
27 +* **CartResolver.getProducts**
28 +* **CartResolver.calculateDiscount**
29 +* **Messenger.sendEmail**
30 +* **Messenger.sendSms**
30 30  
31 -Just by looking at the method names, it's clear what your server can do.
32 +Even just by looking at the method names, it is clear what your server can do.
32 32  
33 -This approach allows for the use of identical method names in different classes:
34 +This approach allows you to use the same method names in different classes:
34 34  
35 -* **//ProductService.create//**
36 -* **//ProductService.getName//**
37 -* **//UserService.create//**
38 -* **//UserService.getName//**
39 -{{/success}}
36 +* **ProductService.create**
37 +* **ProductService.getName**
38 +* **UserService.create**
39 +* **UserService.getName**
40 +)))
40 40  
41 -= Getting Started =
42 += Simple Start =
42 42  
43 -The following example demonstrates how easy it is to add your methods to the API.
44 +The following example should demonstrate the ease of adding your methods to the API.
44 44  
45 45  (% class="row" %)
46 46  (((
... ... @@ -57,30 +57,28 @@
57 57   public function __construct(
58 58   // connecting some dependencies to retrieve data
59 59   ) {}
60 -
61 +
61 61   public function getUserNameByUuid(
62 62   string $userId
63 - ): string
64 - {
65 - // some logic get user info by id
64 + ): string {
65 + // some logic to 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 - {
75 - // some logic send email
73 + ): bool {
74 + // some logic to send email
76 76   return true;
77 77   }
78 78  }
79 79  {{/code}}
80 80  
81 -To the right is an example of the documentation that the server will generate for this class.
80 +To the right, you see an example of the documentation that the server will generate for this class.
82 82  
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.
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.
84 84  )))
85 85  
86 86  (% class="col-xs-12 col-sm-6" %)
... ... @@ -87,47 +87,47 @@
87 87  (((
88 88  {{code language="json" layout="LINENUMBERS" title="== Documentation =="}}
89 89  {
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
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
100 100   }
101 101   },
102 - "returns": "string",
103 - "responseFormat": "string"
101 + "returns": "string",
102 + "responseFormat": "string"
104 104   },
105 - "ExampleApi.sendEmail": {
106 - "name": "ExampleApi.sendEmail",
107 - "description": "",
108 - "parameters": {
109 - "email": {
110 - "type": "string",
111 - "name": "email",
112 - "description": "",
113 - "optional": false
104 + "ExampleApi.sendEmail": {
105 + "name": "ExampleApi.sendEmail",
106 + "description": "",
107 + "parameters": {
108 + "email": {
109 + "type": "string",
110 + "name": "email",
111 + "description": "",
112 + "optional": false
114 114   },
115 - "text": {
116 - "type": "string",
117 - "name": "text",
118 - "description": "",
119 - "optional": false
114 + "text": {
115 + "type": "string",
116 + "name": "text",
117 + "description": "",
118 + "optional": false
120 120   },
121 - "subject": {
122 - "type": "string",
123 - "name": "subject",
124 - "description": "",
125 - "optional": true,
126 - "default": "Message without subject"
120 + "subject": {
121 + "type": "string",
122 + "name": "subject",
123 + "description": "",
124 + "optional": true,
125 + "default": "Message without subject"
127 127   }
128 128   },
129 - "returns": "boolean",
130 - "responseFormat": "boolean"
128 + "returns": "boolean",
129 + "responseFormat": "boolean"
131 131   }
132 132   }
133 133  }
... ... @@ -145,10 +145,10 @@
145 145  (((
146 146  {{code language="json" layout="LINENUMBERS" title="Request"}}
147 147  {
148 - "id": "example_request",
149 - "method": "ExampleApi.getUserNameByUuid",
150 - "params": {
151 - "userId": "1111"
147 + "id": "example_request",
148 + "method": "ExampleApi.getUserNameByUuid",
149 + "params": {
150 + "userId": "1111"
152 152   }
153 153  }
154 154  {{/code}}
... ... @@ -158,9 +158,9 @@
158 158  (((
159 159  {{code language="json" layout="LINENUMBERS" title="Response"}}
160 160  {
161 - "id": "example_request",
162 - "result": "some result",
163 - "jsonrpc": "2.0"
160 + "id": "example_request",
161 + "result": "some result",
162 + "jsonrpc": "2.0"
164 164  }
165 165  {{/code}}
166 166  )))
... ... @@ -172,12 +172,12 @@
172 172  (((
173 173  {{code language="json" layout="LINENUMBERS" title="Request"}}
174 174  {
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"
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"
181 181   }
182 182  }
183 183  {{/code}}
... ... @@ -187,9 +187,9 @@
187 187  (((
188 188  {{code language="json" layout="LINENUMBERS" title="Response"}}
189 189  {
190 - "id": "example_request",
191 - "result": true,
192 - "jsonrpc": "2.0"
189 + "id": "example_request",
190 + "result": true,
191 + "jsonrpc": "2.0"
193 193  }
194 194  {{/code}}
195 195  )))
... ... @@ -197,14 +197,14 @@
197 197  
198 198  (% class="box warningmessage" %)
199 199  (((
200 -To simplify the understanding of the documentation, all subsequent examples will be provided using a single method (//**sendEmail**//).
199 +To simplify the documentation, all further examples will be provided using a single method (//**sendEmail**//).
201 201  )))
202 202  
203 -= Method and Parameter Descriptions =
202 += Description of Methods and Parameters =
204 204  
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.
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.
206 206  
207 -Let's add a description of the method and parameters.
206 +Let's add a description for the method and its parameters.
208 208  
209 209  (% class="row" %)
210 210  (((
... ... @@ -213,7 +213,6 @@
213 213  {{code language="php" layout="LINENUMBERS" title="== Code =="}}
214 214  <?php
215 215  // ...
216 -
217 217   /**
218 218   * A method for sending an email message
219 219   * @param string $email The email address
... ... @@ -222,21 +222,17 @@
222 222   * @return bool
223 223   */
224 224   public function sendEmail(
225 - string $email,
226 - string $text,
223 + string $email,
224 + string $text,
227 227   string $subject = 'Message without subject'
228 - ): bool
229 - {
230 - // some logic send email
226 + ): bool {
227 + // some logic to send email
231 231   return true;
232 232   }
233 -
234 234  // ...
235 235  {{/code}}
236 236  
237 237  Note the documentation now displays additional information about the method and its parameters.
238 -
239 -
240 240  )))
241 241  
242 242  (% class="col-xs-12 col-sm-6" %)
... ... @@ -243,33 +243,33 @@
243 243  (((
244 244  {{code language="json" layout="LINENUMBERS" title="== Documentation =="}}
245 245  {
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
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
256 256   },
257 - "text": {
258 - "type": "string",
259 - "name": "text",
260 - "description": "Message body",
261 - "optional": false
251 + "text": {
252 + "type": "string",
253 + "name": "text",
254 + "description": "Message body",
255 + "optional": false
262 262   },
263 - "subject": {
264 - "type": "string",
265 - "name": "subject",
266 - "description": "Optional message subject parameter",
267 - "optional": true,
268 - "default": "Message without subject"
257 + "subject": {
258 + "type": "string",
259 + "name": "subject",
260 + "description": "Optional message subject parameter",
261 + "optional": true,
262 + "default": "Message without subject"
269 269   }
270 270   },
271 - "returns": "boolean",
272 - "responseFormat": "boolean"
265 + "returns": "boolean",
266 + "responseFormat": "boolean"
273 273   }
274 274   }
275 275  }
... ... @@ -277,18 +277,17 @@
277 277  )))
278 278  )))
279 279  
280 -= RPC attributes =
274 += RPC Attributes =
281 281  
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]].
276 +For more flexible customization of your API methods, JsonRpcBundle uses [[PHP attributes>>url:https://www.php.net/manual/en/language.attributes.overview.php]].
283 283  
284 -With specialized attributes, you can:
278 +With the help of specialized attributes, you can:
285 285  
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.
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.
290 290  
291 -Learn more about these attributes:
285 +Read more about these attributes:
292 292  
293 293  {{children/}}
294 -