star-insure / api-client
Star Insure API 客户端
7.0.0
2024-05-31 04:14 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^10.1
- dev-master
- 7.0.0
- 6.4.1
- 6.4.0
- 6.3.1
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.21
- 5.0.20
- 5.0.19
- 5.0.18
- 5.0.17
- 5.0.16
- 5.0.15
- 5.0.14
- 5.0.13
- 5.0.12
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.1.0
- 4.0.0
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.7.0
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.1
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-SI-204-use-spatie-once-for-memoizing-request-user
This package is auto-updated.
Last update: 2024-10-01 00:10:48 UTC
README
一个为Laravel应用提供的包,它包括对Star Inure API的封装,并为与Star auth应用进行身份验证提供了路由、控制器和中间件模板。
安装
您可以通过composer安装此包
composer require star-insure/api-client
将这些值添加到您的.env
文件中
# API SIS_API_URL=http://api.starinsure.test SIS_API_AUTH_STRATEGY=user|app SIS_API_TOKEN=dev SIS_API_GROUP_ID=2 # OAuth client SIS_API_CLIENT_ID=app SIS_API_CLIENT_SECRET=secret
发布配置
php artisan vendor:publish --tag=starinsure
追加路由
将以下行添加到您的/routes/web.php
文件中。
require __DIR__.'/oauth.php';
使用方法
API
通过实例化一个新的客户端,或使用StarInsure\Api\Facades\StarApi
门面来调用Star API。
StarApi::get('/users/me'); StarApi::get('/users');
身份验证
使用Laravel提供的"auth"中间件。
Route::get('/protected', function () { return 'Only authenticated users can see this.'; })->middleware('auth');
或路由组
Route::middleware(['auth'])->group(function () { Route::get('/protected', function () { return "Only authenticated users can see this."; }); });
如果没有有效会话,用户将被自动重定向到身份验证服务器,登录成功后会返回到请求的目的地。
辅助函数
遵循Laravel风格,您还可以使用辅助函数。要注册身份验证辅助函数以覆盖Laravel内置的auth()
函数,请按照以下步骤操作。
此包依赖于funkjedi/composer-include-files
,允许您在依赖项的全局函数之前加载自己的函数。
在app
目录中创建一个helpers.php
文件(或编辑现有的一个)
// We use this helper purely to help the IDE recognise the global function's return type if (! function_exists('auth') && isset($_ENV['APP_ENV']) && $_ENV['APP_ENV'] === 'ide') { /** * Get the available auth instance. */ function auth(): StarInsure\Api\StarAuthManager { return new \StarInsure\Api\StarAuthManager( app(), config('star.api_url').'/api/'.config('star.version'), ); } } if (! function_exists('api')) { /** * Global helper to create an instance of the StarApi client. */ function api(string $token = null) { return new \StarInsure\Api\StarApi( auth_strategy: config('star.auth_strategy'), version: config('star.version'), apiTokenOverride: $token, ); } } if (! function_exists('appApi')) { /** * An instance of the API client for non-authenticated routes */ function appApi() { return new \StarInsure\Api\StarApi( 'app', config('star.version'), config('star.token'), config('star.group_id') ); } }
在composer.json
中自动加载辅助函数文件
"autoload": { ... "files": [ "app/helpers.php" ] },
在将辅助函数文件添加到composer.json后,您需要导出自加载器
composer dump-autoload
现在您可以使用全局辅助函数,而不必担心命名空间/导入。
$user = auth()->user(); $id = auth()->id(); $group = auth()->group(); $role = auth()->role(); $permissions = auth()->permissions(); $context = auth()->context(); $apiResponse = api()->get('/users/me', [ 'include' => 'groups' ]);