star-insure/api-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

star-insure/api-client

Composer 安装命令:

composer require star-insure/api-client

包简介

Star Insure API Client

关键字:

README 文档

README

A package for Laravel apps that includes a wrapper for the Star Inure API and scaffolds out routes, controllers and middleware for authenticating with the Star auth app.

Installation

You can install the package via composer:

composer require star-insure/api-client

Add these values to your .env file:

# 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

Publish config:

php artisan vendor:publish --tag=starinsure

Append routes

Add the below line to your /routes/web.php file.

require __DIR__.'/oauth.php';

Usage

API

Call the Star API by instantiating a new client, or using the StarInsure\Api\Facades\StarApi facade.

StarApi::get('/users/me');
StarApi::get('/users');

Auth

Use Laravel's provided "auth" middleware.

Route::get('/protected', function () {
    return 'Only authenticated users can see this.';
})->middleware('auth');

Or a route group:

Route::middleware(['auth'])->group(function () {
    Route::get('/protected', function () {
        return "Only authenticated users can see this.";
    });
});

The user will automatically be redirected to the auth server if no valid session, and sent back to their request destination after successfully logging in.

Helper functions

Following the Laravel style, you also have the option of using helper functions. To register the auth helper so it overrides Laravel's built-in auth() functions, follow the steps below.

This package has a dependency on funkjedi/composer-include-files, which allows you to load your own functions prior to any of your dependencies global functions.

Create a helpers.php file within the app directory (or edit your existing one):

// 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')
        );
    }
}

Autoload your helpers file in composer.json:

"autoload": {
    ...
    "files": [
        "app/helpers.php"
    ]
},

After adding the helpers file to composer.json, you'll need to dump the autoloader

composer dump-autoload

You can now use the global helper functions and not worry about namespaces/imports.

$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' ]);

star-insure/api-client 适用场景与选型建议

star-insure/api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 623 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「star-insure」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 star-insure/api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 star-insure/api-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 star-insure/api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

与 star-insure/api-client 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

  • 总下载量: 623
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-12