rasclatt/smart-api 问题修复 & 功能扩展

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

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

rasclatt/smart-api

Composer 安装命令:

composer require rasclatt/smart-api

包简介

Class Library For the Nubersoft API Service

README 文档

README

Basic API Service using the Nubersoft and Smart DTO Libraries

<?php
use \SmartApi\ {
    App
};

use \SmartApi\Models\ {
    Auth,
    Auth\JWT,
    App as SetUp,
    ErrorHandler
};

use \SmartApi\Dto\ {
    Auth\Validate
};

use \SmartDto\Dto;

use \Nubersoft\ {
    nQuery as Db
};

try {
    # Set default return keys as camel case
    App::$responseType = 'c';
    # Create an alternate override folder for calling data
    App::addCoreClass('\\SomeClass');
    # Create an alternate override folder for calling data
    App::addCoreDtoClass('\\SomeDtoClass');
    # Start API
    $Api = new App();
    # Set up the response headers and fetch the bearer
    $SetUp = $Api->setUp(
        new SetUp(),
        # Create a catchall for errors and report inside API
        new ErrorHandler(function($errCode, $errMsg) {
            throw new \SmartApi\Exception($errMsg, $errCode);
        }, E_ALL)
    );
    # Use the internal user authenticator
    $Api->useAuthenticator(
       ...[
            # Internal use authenticator class
            new Auth(...[
                    # Db to fetch internal user
                    new Db(),
                    # Validate object using bearer
                    $validator = new Validate($SetUp->headers)
                ]
            ),
            # Validate a JWT token
            new JWT($validator)
        ]
    );
    # Allows a callable do before action
    //$Api->beforeInit(function(\Nubersoft\nApp $nApp) use ($Api) {
    //});
    # Run the API
    $response = $Api->init();
    # If smart dto, allow different key return formats
    if($response instanceof Dto) {
        switch($Api->getReturnType()) {
            case('c'):
                $data = $response->toCamelCase();
                break;
            case('p'):
                $data = $response->toPascalCase();
                break;
            default:
                $data = $response->toArray();
        }
    }
    else {
        $data = $response;
    }
}
catch (\SmartApi\Exception $e) {
    $e->halt();
}
catch (\Exception $e) {
    $data = [
        'error' => 'An program error occurred',
        'code' => $e->getCode()
    ];
}
# Write out the response
$this->ajaxResponse($data);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固