timiki/rpc-server-bundle
Composer 安装命令:
composer require timiki/rpc-server-bundle
包简介
JSON-RPC server bundle for symfony
关键字:
README 文档
README
JSON-RPC is a remote procedure call protocol encoded in JSON. It is a very simple protocol (and very similar to XML-RPC) , defining only a handful of data types and commands. JSON-RPC allows for notifications (data sent to the server that does not require a response) and for multiple calls to be sent to the server which may be answered out of order.
Install
Symfony >= 7.0 || 8.0
composer require timiki/rpc-server-bundle "^7.0"
Symfony >= 6.4 || 7.0
composer require timiki/rpc-server-bundle "^6.4"
Symfony >= 6.0
composer require timiki/rpc-server-bundle "^6.0"
Deprecated versions
Symfony >= 5.0 use version ^5.0
composer require timiki/rpc-server-bundle "^5.0"
Symfony >= 4.3 use version ^4.1
composer require timiki/rpc-server-bundle "^4.1"
Symfony < 4.3 use version ^4.0
composer require timiki/rpc-server-bundle "^4.0"
Configs
rpc_server: # Mapping configs # Default mapping # mapping: '%kernel.project_dir%/src/Method' # # Multi dir mapping # mapping: # - '%kernel.project_dir%/src/Method1' # - '%kernel.project_dir%/src/Method2' # # Multi handler|dir mapping # mapping: # v1: # - '%kernel.project_dir%/src/Method/V1' # v2: # - '%kernel.project_dir%/src/Method/V2' # mapping: '%kernel.project_dir%/src/Method' # Cache pool name cache: null # Serializer service, must be instanced of Timiki\Bundle\RpcServerBundle\Serializer\SerializerInterface # By default use Timiki\Bundle\RpcServerBundle\Serializer\BaseSerializer serializer: null parameters: # Allow extra params in JSON request allow_extra_params: false
Add methods dir to exclude from autowire
App\: resource: '../src/*' exclude: '../src/{Method}'
Controller
Default:
You can use you own controller for JSON-RPC request. For example:
<?php declare(strict_types=1); namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; class RpcController extends AbstractController { public function indexAction(Request $request) { return $this->get('rpc.server.http_handler.default')->handleHttpRequest($request); } }
or add default JSON-RPC route (default POST to /rpc) to you routing.yml
rpc: path: /rpc defaults: { _controller: Timiki\Bundle\RpcServerBundle\Controller\RpcController::handlerAction } methods: [POST]
or controller for different handler (version)
rpc: path: /{version} defaults: { _controller: Timiki\Bundle\RpcServerBundle\Controller\RpcController::handlerAction, version: v1 } methods: [POST]
If web site and JSON-RPC server located on a different domain remember about CORS.
Method
<?php declare(strict_types=1); namespace App\Method; use Timiki\Bundle\RpcServerBundle\Attribute as RPC; use Symfony\Component\Validator\Constraints as Assert; #[RPC\Method("name")] #[RPC\Roles(["ROLE_NAME"])] #[RPC\Cache(3600)] class Method { #[RPC\Param] #[Assert\NotBlank] protected $param; #[RPC\Execute] public function execute() { $param = $this->param; ... return $result; } }
Or you can also use __invoke to declare a call method
<?php declare(strict_types=1); namespace App\Method; use Timiki\Bundle\RpcServerBundle\Attribute as RPC; use Symfony\Component\Validator\Constraints as Assert; #[RPC\Method("name")] #[RPC\Roles(["ROLE_NAME"])] #[RPC\Cache(3600)] class Method { #[RPC\Param] #[Assert\NotBlank] protected $param; public function __invoke() { $param = $this->param; ... return $result; } }
Inject method execute Context
<?php declare(strict_types=1); namespace App\Method; use Timiki\Bundle\RpcServerBundle\Attribute as RPC; use Timiki\Bundle\RpcServerBundle\Method\Context; use Symfony\Component\Validator\Constraints as Assert; #[RPC\Method("name")] #[RPC\Roles(["ROLE_NAME"])] #[RPC\Cache(3600)] class Method { #[RPC\Param] #[Assert\NotBlank] protected $param; public function __invoke(Context $context) { $param = $this->param; ... return $result; } }
Attributes
Method
Define class as JSON-RPC method.
#[Method("name")]
Roles
Set roles for access to method. If user not granted for access server return error with message "Method not granted" and code "-32001".
#[Roles(["ROLE_NAME", "ROLE_OTHER"])]
Cache
If define cache in configs it set response lifetime.
#[Cache(3600)]
Param
Define JSON-RPC params. Use Symfony\Component\Validator\Constraints for validate it.
#[Param] protected $param; #[Param] protected $param = null'; // Default value for param
Execute
Define execute function in class.
#[Execute] public function execute() { // Code }
or use __invoke
public function __invoke() { // Code }
Serialize
For convert output result from method to json in bundle used serializer
Config for serializer:
rpc: serializer: rpc.server.serializer.base # serialize service id
In bundle include next serializers:
rpc.server.serializer.base - (default) use Symfony serialize to convert result to json rpc.server.serializer.role - use user roles as @Group (@see https://symfony.com/doc/current/components/serializer.html) for control access to output array
Create custom serializer
Here is an example of a simple class for serialization.
<?php declare(strict_types=1); namespace App\Serializer; use Timiki\Bundle\RpcServerBundle\Serializer\SerializerInterface; class MySerializer implements SerializerInterface { public function serialize(mixed $data): string // You serialize logic } public function toArray(mixed $data): array // You serialize logic } }
And then add custom serializer service id to config
rpc: serializer: MySerializer # serialize service id
timiki/rpc-server-bundle 适用场景与选型建议
timiki/rpc-server-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 89k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2016 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rpc」 「json-rpc」 「symfony-bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 timiki/rpc-server-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 timiki/rpc-server-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 timiki/rpc-server-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Microservice RPC through message queues.
Yii2 JSON-RPC server implementation
The bundle for easy using json-rpc api on your project
Symfony bundle for the Rekapager library
Symfony bundle for Cloudflare integration: trusted proxies, real client IP detection, forwarded headers.
A simple Odoo JSON-RPC client you can understand
统计信息
- 总下载量: 89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 13
- 依赖项目数: 3
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-18