denagus/ arxservice
ARX平台的HTTP请求核心
1.0.8
2022-04-26 14:14 UTC
Requires
- php: >=7
Requires (Dev)
- guzzlehttp/guzzle: ^6.3
- laravel/laravel: ^5.6
- laravel/lumen: ^5.8
README
ARX Service 是ARX应用程序用于在服务之间进行HTTP请求的包,具有身份验证和安全访问功能
支持的版本
版本 | Laravel版本 |
---|---|
> 1.0.0 | >=5.7 |
安装方法
composer require denagus/arxservice
##### Laravel
- 由
Laravel Package Discovery
自动注册
##### Lumen
- 在
bootstrap/app.php
中注册服务提供者$app->register(Denagus\ArxService\ArxServiceProvider::class);
配置设置
- 由
创建文件
config/arxservice.php
<?php return [ /* |-------------------------------------------------------------------------- | Encryption Key |-------------------------------------------------------------------------- | | This key is used by the ARX Service and should be set | to a random, 32 character string, otherwise these encrypted strings | will not be safe. Please do this before deploying an application! | */ 'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC', /* |-------------------------------------------------------------------------- | Hash |-------------------------------------------------------------------------- | | This key is used by the ARX Service | */ 'hash' => 'sha256', /* |-------------------------------------------------------------------------- | Expire |-------------------------------------------------------------------------- | | The expire time is the number of seconds that the access key should be | considered valid. This security feature keeps access keys short-lived so | they have less time to be guessed. You may change this as needed. | */ 'expire' => 14400 ];
- 注册配置文件#### Laravel
- 自动注册#### Lumen
- 在
app/bootstrap.php
中注册配置文件$app->configure('arxservice');
设置中间件
- 确保每个服务请求都使用中间件
service
#### Laravel- 在
app/Http/Kernel.php
中注册中间件服务/** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ 'service' => \Denagus\ArxService\Middleware\Accesskey::class, ];
#### Lumen
- 在
app/bootstrap.php
中注册中间件服务$app->routeMiddleware([ 'service' => Denagus\ArxService\Middleware\Accesskey::class, ]);
- 在
在
controller
文件中使用中间件的示例/** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('service'); }
设置服务
- 在路径
app/Libraries/Services
上创建文件夹 - 创建文件
UserService.php
<?php
namespace App\Libraries\Services;
use Denagus\ArxService\Service;
class UserService extends Service {
/** * SERVICE_USER_URI */ protected $baseUri = 'SERVICE_USER_URI'; /** * Available URL */ private const USER_BY_ID = 'api/v1/user/service/by-user-id';
}
3. Buat file `AuthService.php`
<?php
namespace App\Libraries\Services;
use Denagus\ArxService\Service;
class AuthService extends Service {
/** * SERVICE_USER_URI */ protected $baseUri = 'SERVICE_AUTH_URI';
}
3. Tambahkan `SERVICE_USER_URI` pada .env, dan atur URI dari masing-masing service
SERVICE_USER_URI=https:// SERVICE_AUTH_URI=https://
- 在路径
使用方法
请求服务用户/
UserService
use App\Libraries\Services\UserService; use Denagus\ArxService\Exception as ServiceException; try { $userService = UserService::get(UserService::USER_BY_ID . "/1"); ServiceException::on($userService); $user = $userService->data; } catch (\Exception $e) { $user = null; }