1
1
# JSON-RPC 1.0 + 2.0 Handler
2
2
3
3
The class ` Vectorface\SnappyRouter\Handler\JsonRpcHandler ` provides a means of
4
- calling class methods via the JSON-RPC protocol. Version 1.0 and 2.0 of the
5
- protocol should both be fully supported.
4
+ calling class methods via the
5
+ [ JSON-RPC] ( http://json-rpc.org/wiki/specification ) protocol. Version 1.0 and
6
+ 2.0 of the protocol should both be fully supported.
6
7
7
8
Some items of interest about this implementation:
9
+
8
10
* Supports batch calls; Many calls in a single request.
9
11
* Notification calls; Drops responses without a request id.
10
12
* Handles both parameter arrays, and named parameters.
11
13
* Transparently handles both the JSON-RPC 1.0 and 2.0 spec.
12
- * JSON-RPC 1.0 class hinting * is not supported* . (On purpose.)
14
+ * JSON-RPC 1.0 class hinting * is delibrately not supported* .
13
15
14
16
## Why JSON-RPC?
15
17
@@ -22,14 +24,16 @@ identical to a local method call, making it very easy to integrate server-side
22
24
calls into client-side or remote code.
23
25
24
26
API clients can also be simpler too because the local and remote method
25
- signatures can be the same. There is no need to map URLs and/or parameters as in
26
- REST APIs.
27
+ signatures can be the same. There is no need to map URLs and/or parameters as
28
+ in REST APIs.
27
29
28
30
For example, one could expose the following class on the server:
29
31
30
32
``` php
31
33
<?php
32
34
35
+ namespace Vendor\MyNamespace\RpcServices;
36
+
33
37
class Adder
34
38
{
35
39
public function add($arg1, $arg2)
@@ -39,22 +43,24 @@ class Adder
39
43
}
40
44
```
41
45
42
- The Adder could be called locally as:
46
+ The class ` Adder ` can be called locally as:
47
+
43
48
``` php
44
- $adder = new Adder();
49
+ $adder = new Vendor\MyNamespace\RpcServices\ Adder();
45
50
$adder->add(1, 1); // 2!
46
51
```
47
52
48
53
Or it could be called remotely:
54
+
49
55
``` php
50
- $adder = new $myJsonRpcClient ("http://.../Adder"); // Any JSON-RPC client.
56
+ $adder = new MyJsonRpcClient ("http://.../Adder"); // Any JSON-RPC client.
51
57
$adder->add(1, 1); // 2!
52
58
```
53
59
54
60
## Usage
55
61
56
- To expose the example Adder class listed in the previous section, one could
57
- configure a router instance as follows:
62
+ To expose the example ` Adder ` class listed in the previous section, configure a
63
+ router instance as follows:
58
64
59
65
``` php
60
66
<?php
@@ -67,7 +73,7 @@ $config = new Config(array(
67
73
Config::KEY_CLASS => 'Vectorface\\SnappyRouter\\Handler\\JsonRpcHandler',
68
74
Config::KEY_OPTIONS => array(
69
75
Config::KEY_SERVICES => array(
70
- 'Adder' => '\Adder' // Adder, as above
76
+ 'Adder' => 'Vendor\\MyNamespace\\RpcServices\ \Adder' // Adder, as above
71
77
),
72
78
)
73
79
)
@@ -78,5 +84,5 @@ echo $router->handleRoute();
78
84
```
79
85
80
86
If the router is called with a URI ending in "Adder(.php)" and a valid JSON-RPC
81
- request POSTed for the method "add", the router should yield a JSON-RPC encoded
82
- response with the sum of the two arguments.
87
+ request POSTed for the method "add", the router will response with a JSON-RPC
88
+ encoded response with the sum of the two arguments.
0 commit comments