PHP code example of idopin / api-support

1. Go to this page and download the library: Download idopin/api-support library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

idopin / api-support example snippets


public function render($request, Throwable $e)
{
    if ($request->is('api/*')) {
        return $this->jsonResponse($e);
    }

    return  parent::render($request, $e);
}

 'api' => [
            'driver' => 'passport',
            'provider' => 'users',
  ]



namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * Find the user instance for the given username.
     *
     * @param  string  $username
     * @return \App\Models\User
     */
    public function findForPassport($username)
    {
        return $this->where('name', $username)->first();
    }
}



namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * Validate the password of the user for the Passport password grant.
     *
     * @param  string  $password
     * @return bool
     */
    public function validateForPassportPasswordGrant($password)
    {
        return Hash::check($password, $this->password);
    }
}

return [
    'ok'                           => [0, 'OK', 200],
    'redirect'                     => [1, '重定向', 301],
    'resource_created'             => [2, '创建新资源', 201],
    'resource_updated'             => [3, '更新了资源', 200],
    'resource_exist'               => [4, '资源已存在', 200],
    'resource_unchanged'           => [5, '资源无变化', 200],
    'no_content'                   => [6, '无返回内容', 204],
    'captcha_exception'            => [200, '验证码异常', 403],
    'captcha_invalid'              => [201, '验证码失效', 403],
    'captcha_incorrect'            => [202, '验证码不正确', 403],
    'sms_code_exception'           => [203, '短信验证码异常', 403],
    'sms_code_invalid'             => [204, '短信验证码失效', 403],
    'sms_code_incorrect'           => [205, '短信验证码不正确', 403],
    'verification_code_exception'  => [200, '验证码异常', 403],
    'verification_code_invalid'    => [201, '验证码失效', 403],
    'verification_code_incorrect'  => [202, '验证码不正确', 403],
    'user_not_exist'               => [300, '用户不存在', 404],
    'user_auth_failed'             => [302, '用户认证失败,用户名或密码不正确', 401],
    'user_create_failed'           => [305, '用户创建失败', 403],
    'user_not_auth'                => [306, '用户未登录', 403],
    'database_query'               => [500, '数据库操作失败', 500],
    'token_invalid'                => [601, 'Token 已失效', 403],
    'form_data_invalid'            => [700, '表单有字段错误', 422],
    'attempt_to_many'              => [800, '请求次数过多', 429],
    'resource_not_found'           => [900, '资源不存在', 404],
    'route_not_found'              => [1000, '路由不存在', 404],
    'undefine'                     => [9999, '未定义的错误', 500],
];



namespace Idopin\ApiSupport\Enums;

enum ApiCode: string
{
    case ATTEMPT_TO_MANY              = 'attempt_to_many';
    case CAPTCHA_EXCEPTION            = 'captcha_exception';
    case CAPTCHA_INCORRECT            = 'captcha_incorrect';
    case CAPTCHA_INVALID              = 'captcha_invalid';
    case DATABASE_QUERY               = 'database_query';
    case FORM_DATA_INVALID            = 'form_data_invalid';
    case NO_CONTENT                   = 'no_content';
    case OK                           = 'ok';
    case REDIRECT                     = 'redirect';
    case RESOURCE_CREATED             = 'resource_created';
    case RESOURCE_EXIST               = 'resource_exist';
    case RESOURCE_NOT_FOUND           = 'resource_not_found';
    case RESOURCE_UNCHANGED           = 'resource_unchanged';
    case RESOURCE_UPDATED             = 'resource_updated';
    case ROUTE_NOT_FOUND              = 'route_not_found';
    case SMS_CODE_EXCEPTION           = 'sms_code_exception';
    case SMS_CODE_INCORRECT           = 'sms_code_incorrect';
    case SMS_CODE_INVALID             = 'sms_code_invalid';
    case TOKEN_INVALID                = 'token_invalid';
    case UNDEFINE                     = 'undefine';
    case USER_AUTH_FAILED             = 'user_auth_failed';
    case USER_CREATE_FAILED           = 'user_create_failed';
    case USER_NOT_AUTH                = 'user_not_auth';
    case USER_NOT_EXIST               = 'user_not_exist';
    case VERIFICATION_CODE_EXCEPTION  = 'verification_code_exception';
    case VERIFICATION_CODE_INCORRECT  = 'verification_code_incorrect';
    case VERIFICATION_CODE_INVALID    = 'verification_code_invalid';
}

     /**
     * Json 内容响应
     *
     * @param Idopin\ApiSupport\Enums\ApiCode $returnCode 返回码
     * @param mixed $result 结果
     * @param null|string|null $message 返回的信息,会覆盖默认的信息
     * @param integer|null $httpCode  HTTP 响应码
     * @return \Illuminate\Http\JsonResponse
     */
    public function response(ApiCode $apiCode = ApiCode::OK, $result = null, string $message = '', int $httpCode = null): JsonResponse{
        ...
    }

    /**
     * 对比响应
     *
     * @param Illuminate\Http\JsonResponse $response
     * @param Idopin\ApiSupport\Enums\ApiCode $apiCode
     * @return boolean
     */
    public function responseEqual(JsonResponse $response, ApiCode $apiCode) :bool{
        ...
    }

    // 调用示例
    $this->responseEqual($response, ApiCode::OK)

 /**
 * Json 内容响应
 *
 * @param Idopin\ApiSupport\Enums\ApiCode $returnCode 返回码
 * @param mixed $result 结果
 * @param null|string|null $message 返回的信息,会覆盖默认的信息
 * @param integer|null $httpCode  HTTP 响应码
 * @return JsonResponse
 */
function api_response(ApiCode $apiCode = ApiCode::OK, $result = null, string $message = '', int $httpCode = null): JsonResponse{
    ...
}

/**
 * 对比响应
 *
 * @param \Illuminate\Http\JsonResponse $response
 * @param \Idopin\ApiSupport\Enums\ApiCode $apiCode
 * @return boolean
 */
function api_response_equal(JsonResponse $response, ApiCode $apiCode): bool{
    ...
}





namespace Idopin\ApiSupport\Traits;

use Idopin\ApiSupport\Enums\ApiCode;
use Throwable;

trait ApiExceptionTrait
{
    use ApiResponseTrait;
    public function jsonResponse(Throwable $e)
    {
        ...
    }
    ...
}

use Idopin\ApiSupport\Middleware\Human;
...
public function __construct()
{
    $this->middleware(Human::class)->only('xxx');
}
sh
php artisan migrate
sh
php artisan authorization:install