upgate/laravel-jsonrpc
Composer 安装命令:
composer require upgate/laravel-jsonrpc
包简介
Laravel JSON-RPC 2.0 Server
README 文档
README
📢 Updates from Ukrainian Open Source Community
🇷🇺 Обращение к гражданам России
Quick How-To
- Install with composer:
composer require upgate/laravel-jsonrpc - In your RouteServiceProvider, do something like this:
// ... use Upgate\LaravelJsonRpc\Contract\ServerInterface as JsonRpcServerContract; class RouteServiceProvider extends ServiceProvider { // ... public function map(Router $router) { $router->group( ['namespace' => $this->namespace], function (Router $router) { // Create an instance of JsonRpcServer $jsonRpcServer = $this->app->make(JsonRpcServerContract::class); // Set default controller namespace $jsonRpcServer->setControllerNamespace($this->namespace); // Register middleware aliases configured for Laravel router $jsonRpcServer->registerMiddlewareAliases($router->getMiddleware()); require app_path('Http/routes.php'); } ); } }
- Use $jsonRpcServer in your routes.php, like this:
$router->post('/jsonrpc', function (Illuminate\Http\Request $request) use ($jsonRpcServer) { $jsonRpcServer->router() ->addMiddlewares(['fooMiddleware', 'barMiddleware', 'auth:rpcGuard']) // Middleware alias names or class names. // Parameters may be specified by separating // the middleware name and parameters with a : ->bindController('foo', 'FooController') // for 'foo.$method' methods invoke FooController->$method(), // for 'foo' method invoke FooConroller->index() ->bind('bar', 'MyController@bar') // for 'bar' method invoke MyController->bar() ->group( ['bazMiddleware'], // add bazMiddleware for methods in this group function ($jsonRpcRouter) { // for 'bar.baz' method invoke MyController->bazz() $jsonRpcRouter->bind('bar.baz', 'MyController@bazz'); } ); // Run json-rpc server with $request passed to middlewares as a handle() method argument return $jsonRpcServer->run($request); });
See ServerTest and RouterTest for more examples.
Exception handling
Descendants of Upgate\LaravelJsonRpc\Exception\JsonRpcException represent JSON-RPC errors according to the spec (see Upgate\LaravelJsonRpc\Server\ErrorCode).
Other exceptions are logged and converted to INTERNAL_ERROR responses by default.
Use Upgate\LaravelJsonRpc\Server\Server::onException() to register custom exception handlers:
use Upgate\LaravelJsonRpc\Server; $jsonRpcServer->onException( SomeExceptionClass::class, function (SomeExceptionClass $e, Server\Request $request = null) { $message = "Some Message"; $code = -32099; return Server\RequestResponse::constructErrorResponse($request ? $request->getId() : null, $message, $code); } ); $jsonRpcServer->onException( \Throwable::class, // catch-all function (\Throwable $e, Server\Request $request = null) { $message = "Some Other Message"; $code = -32098; return Server\RequestResponse::constructErrorResponse($request ? $request->getId() : null, $message, $code); } );
See ServerTest:: testSingleRequestWithExceptionHandler() and ServerTest::testExceptionHandlersPriority() for more examples.
JSON-RPC Request Forms
(since v0.3.0)
Upgate\LaravelJsonRpc\Server\FormRequest is similar to Laravel form requests, but validates JSON-RPC parameters instead.
Example:
use Upgate\LaravelJsonRpc\Server\FormRequest as JsonRpcFormRequest; class MyJsonRpcFormRequest extends JsonRpcFormRequest { public function rules(): array { return [ 'id' => 'required|numeric', 'email' => 'required|email', ]; } } class MyController { public function myAction(MyJsonRpcFormRequest $jsonRpcRequest) { $email = $jsonRpcRequest->email; // or $jsonRpcRequest->get('email'); $allParams = $jsonRpcRequest->all(); // ... } }
If validation fails, the INVALID_PARAMS error response is returned, with validation error details in data.violations.
See the ServerFormRequestTest for more examples.
upgate/laravel-jsonrpc 适用场景与选型建议
upgate/laravel-jsonrpc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45.53k 次下载、GitHub Stars 达 51, 最近一次更新时间为 2015 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「json-rpc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 upgate/laravel-jsonrpc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 upgate/laravel-jsonrpc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 upgate/laravel-jsonrpc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 JSON-RPC server implementation
The bundle for easy using json-rpc api on your project
A simple Odoo JSON-RPC client you can understand
JSON RPC 2.0 for Yii2 API with CRUD+ actions
A library for managing JSON-RPC services
Alfabank REST API integration
统计信息
- 总下载量: 45.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 53
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2015-11-03