PHP code example of kakuilan / hyperf-apihelper

1. Go to this page and download the library: Download kakuilan/hyperf-apihelper 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/ */

    

kakuilan / hyperf-apihelper example snippets


return [
    'http' => [
        Hyperf\Apihelper\Middleware\ApiValidationMiddleware::class,
    ],
];

return [
    'handler' => [
        'http' => [
            Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
            Hyperf\Apihelper\Exception\Handler\ValidationExceptionHandler::class,
            App\Exception\Handler\AppExceptionHandler::class,
        ],
    ],
];

return [
    Hyperf\HttpServer\Router\DispatcherFactory::class => Hyperf\Apihelper\DispatcherFactory::class
];

      public function fn($value, string $field, array $options): array
      

    'base_controller' => Hyperf\Apihelper\Controller\BaseController::class,
    

  @ApiResponse(code=200, schema={"$ref":"Response"})  
  

  public static function getSchemaAbcXyz(): array
  

    // 先定义一个基本的数据模型结构
    public static function getSchemaPerson(): array {
        return [
            'name'   => 'zhang3',
            'age'    => 24,
            'weight' => 64.5,
            'addr'   => 'home',
            'male'   => true,
        ];
    }

    // 再使用$前缀引用其他结构,首字母大小写都可以(但建议大写),组件将会自动转换为首字母大写的驼峰名称.
    public static function getSchemaPersons(): array {
        return [
            '$person',
            '$Person',
            '$Person',
        ];
    }
  

    @ApiResponse(code=200, schema={"$ref":"Response"}, refKey="data", refValue="Company")
    

    public function fn(ServerRequestInterface $request): ServerRequestInterface
    

    public function fn(string $controller, string $action, string $route): mixed
    

    public function fn(ServerRequestInterface $request, ResponseInterface $response): void
    

/**
 * @ApiController(tag="测试实例", description="测试例子")
 * @ApiVersion(group="v1", description="第一版本")
 * @ApiVersion(group="v2", description="第二版本")
 * @ApiVersion(group="v3", description="第三版本")
 */
class Test extends BaseController {}

    $this->request->header('user-agent');
    $this->request->query('len');
    $this->request->post();
    $this->request->route('id');
    $this->request->getUploadedFiles();
    $this->request->getBody()->getContents();
    

  /**
  * @AutoController()
  * @ApiController()
  */
  class Test extends BaseController {}
  

    /**
    * @AutoController()
    */
    class Test extends BaseController {}
    

    /**
    * @ApiController()
    */
    class Test extends BaseController {}
    
sh
# composer安装本组件
composer 到你的项目下
php bin/hyperf.php vendor:publish kakuilan/hyperf-apihelper