katsana/minions
Composer 安装命令:
composer require katsana/minions
包简介
JSON-RPC Communication for Laravel
README 文档
README
Installation
Minions can be installed via composer:
composer require "katsana/minions"
Setup
The package will automatically register a service provider.
Next, you need to publish the Minions configuration file:
php artisan vendor:publish --provider="Minions\MinionsServiceProvider" --tag="config"
Setting Project ID
Each project need to have a unique Project ID to be used to identify authorized RPC requests. You can set the project ID on config/minions.php configuration file:
<?php return [ // ... 'id' => 'project-id', // ... ];
Configure Projects
Next, you need to setup the project clients and servers information:
<?php return [ // ... 'projects' => [ 'client-project-id' => [ 'token' => 'secret-token', 'signature' => 'secret-signature', ], 'server-project-id' => [ 'endpoint' => 'http://server-project-id:8084', 'token' => 'another-secret-token', 'signature' => 'another-secret-signature', ], ], // ... ];
endpointis only required for configurating server project connection from a client project. A server will never send request to a client project.- Each project should have a pair of unique
tokenandsecret, this will be shared only by the client and server as a form of message verification.
Security
token is used as an Authorization bearer token header for the request and signature is used to sign the message sent via the request.
For projects running on private intranet you may skip setting up
tokenandsignatureby setting the value tonull.
Request Handler
To receive a request from a client, first we need to create a request handler on the server, for example let say we want to create a Add request.
<?php namespace App\JsonRpc; use Minions\Http\Request; use Minions\Http\ValidatesRequests; class MathAdd { use ValidatesRequests; /** * Handle the incoming request. * * @param \Minions\Http\Request $request * * @return mixed */ public function __invoke(Request $request) { return \array_sum($request->all()); } /** * Authorize the incoming request. * * @param \Minions\Http\Request $request * * @return bool */ public function authorize(Request $request): bool { return true; } }
You can use
php artisan minions:make MathAddto generate the base stub fileApp\JsonRpc\MathAdd.
Registering the route
To register the route, all you need to do is add the request handler to routes/rpc.php:
<?php use Minions\Router; Router::rpc('math.add', 'App\JsonRpc\MathAdd');
You can run the following command to stub routes/rpc.php:
php artisan vendor:publish --provider="Minions\Http\MinionsServiceProvider" --tag="routes"
Checking authorization
You can check whether the project is authorized to use the request by overriding the authorize() method.
/** * Authorize the incoming request. * * @param \Minions\Http\Request $request * * @return bool */ public function authorize(Request $request): bool { return $request->id() === 'platform'; // only allow access from `platform` }
Validating the request
You can validate the request using Laravel Validation. Minions also introduce Minions\Http\ValidatesRequests trait which you can import and expose validate() and validateWith() method. e.g:
<?php namespace App\JsonRpc; use App\User; use Minions\Http\Request; use Minions\Http\ValidatesRequests; class User { use ValidatesRequests; /** * Handle the incoming request. * * @param \Minions\Http\Request $request * * @return mixed */ public function __invoke(Request $request) { $this->validate($request, [ 'email' => ['required', 'email'], ]); return User::where('email', '=', $request['email'])->firstOrFail(); } }
Making a Request
To make a request, you can create the following code:
<?php use Minions\Client\Message; use Minions\Client\ResponseInterface; use Minions\Minion; Minion::broadcast('server-project-id', Minion::message( 'math.add', [1, 2, 3, 4] ))->then(function (ResponseInterface $response) { assert(10, $response->getRpcResult()); }); Minion::run();
Running the RPC Server
To run Minions RPC Server, you have two options:
- ReactPHP RPC Server using katsana/minions-server
- Laravel Routing (Polyfill) RPC Server using katsana/minions-polyfill
Please go through each option documentation for installation and usages guide.
katsana/minions 适用场景与选型建议
katsana/minions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 90.51k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2019 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 katsana/minions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 katsana/minions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 90.51k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 10
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-10