Wiki source code of #[RPC\Info]
Last modified by Ashterix on 2024/05/19 21:27
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
4.1 | 1 | {{box cssClass="floatinginfobox"}} |
![]() |
1.5 | 2 | == (% style="display:block; margin-top:-30px; text-align:center" %)Summary(%%) == |
![]() |
1.4 | 3 | |
![]() |
1.2 | 4 | (% style="margin-right:auto" %) |
![]() |
5.1 | 5 | |**Classname**|(% colspan="2" rowspan="1" %)Info |
6 | |**Namespace**|(% colspan="2" %)Ufo\RpcObject\RPC | ||
7 | |**Target**|(% colspan="2" rowspan="1" style="width:79px" %)class | ||
8 | |(% colspan="3" %)**Arguments:** | ||
9 | |(% colspan="1" rowspan="3" %)**$alias**|**type**|string | ||
10 | |**optional**|true | ||
11 | |**default**|null | ||
12 | |(% colspan="1" rowspan="3" %)**$concat**|**type**|string | ||
13 | |**optional**|true | ||
14 | |**default**|'.' | ||
![]() |
1.2 | 15 | {{/box}} |
![]() |
3.2 | 16 | |
![]() |
6.1 | 17 | = Class Aliases = |
![]() |
1.5 | 18 | |
![]() |
6.1 | 19 | Sometimes it may be necessary to set an alternative name for an API method, for example, if you have a very long class name due to some standards or naming conventions in your project. |
![]() |
1.2 | 20 | |
![]() |
6.1 | 21 | In such cases, you can add the attribute {{code language="none"}}#[RPC\Info]{{/code}} to your class, specifying an alias for the class. This will instruct the RPC server to associate this alias with the class. |
![]() |
1.2 | 22 | |
23 | {{code language="php" layout="LINENUMBERS"}} | ||
24 | <?php | ||
25 | namespace App\Api\Procedures; | ||
26 | |||
27 | use Ufo\RpcObject\RPC; | ||
28 | |||
![]() |
1.3 | 29 | #[RPC\Info(alias: 'users')] |
![]() |
1.2 | 30 | class MySpecificApiUserServiceWithLongClassName implements IRpcService |
31 | { | ||
![]() |
1.3 | 32 | public function getList(): void {} |
![]() |
1.2 | 33 | } |
34 | {{/code}} | ||
![]() |
1.3 | 35 | |
![]() |
6.1 | 36 | In this case, the API method {{code language="none"}}users.getList{{/code}} will be available. |
![]() |
1.3 | 37 | |
38 | {{warning}} | ||
![]() |
6.1 | 39 | Note that the original naming of the method will be removed from the ServiceMap and will no longer be accessible. |
![]() |
1.3 | 40 | |
![]() |
6.1 | 41 | That is, //**MySpecificApiUserServiceWithLongClassName.getList**// will no longer work! |
![]() |
1.3 | 42 | {{/warning}} |
43 | |||
![]() |
6.1 | 44 | = Concatenation Symbol = |
![]() |
1.5 | 45 | |
![]() |
6.1 | 46 | The Info attribute also accepts a second argument {{code language="none"}}concat{{/code}}, which specifies the character or characters that join the class name and method name when generating the ServiceMap for the RPC server. |
![]() |
1.5 | 47 | |
![]() |
6.1 | 48 | The default value is "." (dot), but you can easily change it by specifying the symbol or characters you need. |
![]() |
1.5 | 49 | |
50 | {{code language="php" layout="LINENUMBERS"}} | ||
51 | <?php | ||
52 | namespace App\Api\Procedures; | ||
53 | |||
54 | use Ufo\RpcObject\RPC; | ||
55 | |||
56 | #[RPC\Info(alias: 'users', concat: '>')] | ||
57 | class MySpecificApiUserServiceWithLongClassName implements IRpcService | ||
58 | { | ||
59 | public function getList(): void {} | ||
60 | } | ||
61 | {{/code}} | ||
62 | |||
![]() |
6.1 | 63 | The example code above will result in the API method {{code language="none"}}users>getList{{/code}} being available. |
![]() |
1.5 | 64 | |
![]() |
6.1 | 65 | = Caveats = |
![]() |
1.5 | 66 | |
![]() |
6.1 | 67 | {{warning}} |
68 | (% id="cke_bm_754S" style="display:none" %) (%%)NOTE that there is no additional validation regarding the use of aliases or concatenation symbols, so your actions could lead to the aliasing of another existing class. | ||
![]() |
1.5 | 69 | |
![]() |
6.1 | 70 | For example, if in your application there is already a class {{code language="none"}}Foo{{/code}} with the method {{code language="none"}}bar{{/code}} added to the API as the method {{code language="none"}}Foo.bar{{/code}}, and you create a class {{code language="none"}}Baz{{/code}} (% id="cke_bm_372757S" style="display:none" %) (%%)with the method {{code language="none"}}bar{{/code}}(% id="cke_bm_372757E" style="display:none" %) (%%) and set its alias to {{code language="none"}}Foo{{/code}}, this will lead to the registration of two API services with the same name. The outcome of these actions is unpredictable. |
71 | {{/warning}} | ||
![]() |
1.5 | 72 | |
73 | {{error}} | ||
![]() |
6.1 | 74 | By using this mechanism, you act at your own risk! |
![]() |
1.5 | 75 | {{/error}} |
76 | |||
![]() |
6.1 | 77 | = In future = |
![]() |
3.2 | 78 | |
![]() |
6.1 | 79 | In the future, we plan to implement the ability to set aliases for each specific API method, not just classes. |