hecsedli/cakephpjsonrpcserver

CakePHP 3.x JSON-RPC 服务器

0.3 2018-05-24 21:23 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:21:11 UTC


README

CakePHP 3.x JSON-RPC 服务器

原始想法:James Watts https://github.com/jameswatts/cake-jsonrpc

需求

  • CakePHP 3.6+
  • PHP 5.6+

安装

您可以使用 composer 安装。

composer require hecsedli/cakephpjsonrpcserver
// config/bootstrap.php

Plugin::load('JSONRPCServer', ['bootstrap' => true]);

实现

将组件添加到控制器中

public $components = ["JSONRPCServer.JsonrpcServer"];
public function user($request) {
	if (isset($request->params->userId)) {
		return $this->User->findById($request->params->userId);
	} else {
		throw new Exception('No user ID was specified', 123);
	}
);

JSON 请求

{
	"jsonrpc": "2.0", 
	"method": "user", 
	"params": {
		"userId": 5
	}, 
	"id": "test"
}