slava-basko/symphony-json-rpc-server

Simple JsonRpc server for symfony

dev-master 2017-02-02 08:00 UTC

This package is auto-updated.

Last update: 2024-10-27 02:04:31 UTC


README

Install

Install via composer and add to kernel.

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            ...
            new JsonRpcServerBundle\JsonRpcBundle(),
        ];
        return $bundles;
    }
    ...

Add this to your routing.yml (or xlm, etc.).

json_rpc_server:
    path: /api
    defaults:  { _controller: JsonRpcBundle:Server:process }

Done!

Usage

Just mark your service with "rpc.service" tag.

rpc_ping:
    class: RpcService\PaymentService
    tags:
        - { name: rpc.service }

Advanced Configuration

You can use server as a service. Example:

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     * @param Request $request
     * @return JsonResponse
     */
    public function indexAction(Request $request)
    {
        // Some Logic. Auth, pre-actions, etc.
    
        $rpcServer = $this->get('rpc_server');
        if ($request->isMethod('get')) {
            return new JsonResponse($rpcServer->getServiceMap()->toArray());
        }
        return new JsonResponse($rpcServer->handle()->toJson(), 200, [], true);
    }
}