zored / speech
JSON-RPC 2.0 for PHP. With Symfony 2.0 to 3.0 support.
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=5.6
- jms/serializer-bundle: ~1.0
- symfony/dependency-injection: ~2.8|~3.0
- symfony/event-dispatcher: ~2.3|~3.0
- symfony/property-info: ~2.8|~3.0
- symfony/validator: ~2.0|~3.0
Requires (Dev)
- phpunit/phpunit: ~4.8
This package is not auto-updated.
Last update: 2024-10-27 02:51:01 UTC
README
JSON-RPC 2.0 for PHP
Bundle for JSON-RPC 2.0. You can use as standalone library. Or with Symfony.
Features
- Validation. Based on Symfony Validator component.
- Deserialization. Based on JMS.
- Customization. Change your rules for JSON-RPC 2.0.
Use
Symfony
- Install package:
composer require zored/speech
. - Add bundle
new Zored\SpeechBundle\ZoredSpeechBundle()
to your Symfony kernelapp/AppKernel.php
. - Add route to default endpoint:
routing.yml
:json_rpc: defaults: { _controller: ZoredSpeechBundle:Default:jsonRpc } methods: [POST] # Or GET for Zored RPC. path: /json-rpc
- Add service with your JSON-RPC methods in
services.yml
:services: your.service: # Replace with your own class: class: 'Zored\SpeechBundle\Test\JsonRpc\Greeter'
- Allow this service to run in default controller via
parameters.yml
:parameters: zored.speech.controller.context.service_ids: ['your.service']
- Clear caches.
- Test:
curl http://example.com/json-rpc/ \ --data '{"id":"1", "method":"your.service:greet", "params":{"person":{"name":"bob","age":19}},"jsonrpc":"2.0"}'
Standalone library
- See example in
web/index.php
- Run there server with
php -S 127.0.0.1:8080
- Test:
curl http://127.0.0.1:8080 \ --data '{"id":"1", "method":"your.service:greet", "params":{"person":{"name":"bob","age":19}},"jsonrpc":"2.0"}'
What's next?
- See default controller if you need multiple JSON-RPC endpoints.
- See tests to know how it works.
Inside
- Your request JSON string goes to endpoint which handles it and returns Symfony response.
- Endpoint chooses which request passer can pass JSON string to request handler.
- Request passer also deserializes JSON to object and validates it.
- Request passer passes request object to request handler that pushes request somewhere to get response.
- Now service handler is used to:
- Deserialize request
params
if set with parameter converter. - Call public method of service with these params. Services are restricted with
ServiceContext
. For default controller seezored.speech.controller.context
parameter. - Wait for
array
orAbstractResponse
.
- Deserialize request
- Now service handler is used to:
- Request then passed back and serialized to array.
Events
You can see available events here.