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
];
namespace App\Controller;
use Hyperf\Apihelper\Annotation\ApiController;
use Hyperf\Apihelper\Annotation\ApiResponse;
use Hyperf\Apihelper\Annotation\ApiVersion;
use Hyperf\Apihelper\Annotation\Method\Delete;
use Hyperf\Apihelper\Annotation\Method\Get;
use Hyperf\Apihelper\Annotation\Method\Patch;
use Hyperf\Apihelper\Annotation\Method\Post;
use Hyperf\Apihelper\Annotation\Method\Put;
use Hyperf\Apihelper\Annotation\Param\Body;
use Hyperf\Apihelper\Annotation\Param\File;
use Hyperf\Apihelper\Annotation\Param\Form;
use Hyperf\Apihelper\Annotation\Param\Header;
use Hyperf\Apihelper\Annotation\Param\Path;
use Hyperf\Apihelper\Annotation\Param\Query;
use Hyperf\Apihelper\Controller\BaseController;
use Hyperf\Apihelper\Exception\ValidationException;
/**
* @ApiController(tag="测试实例", prefix="/hello", description="测试例子")
* @ApiVersion(group="v1", description="第一版本")
*/
class Test extends BaseController {
/**
* @Get(path="/user", summary="接口摘要", description="获取用户详情")
* @Query(key="id", rule=" true,
$value,
];
if (!true) {
$res = [
false,
'具体验证失败的信息',
];
}
return $res;
}
/**
* @Get(description="生成验证码")
* @Header(key="authorization|访问令牌", rule="se(code=200, schema={"$ref":"Response"})
*/
public function testbody() {
return self::doSuccess(date('Y-m-d H:i:s'));
}
/**
* 自定义解析body数据
* @param mixed $value 字段值
* @param string $field 字段名
* @param array $options 参数选项
* @return array
* @throws ValidationException
*/
public function parseBody($value, string $field, array $options): array {
$arr = json_decode($value, true);
$res = [
true,
$value,
];
//自行检查
if (!isset($arr['hello'])) {
$res = [
false,
'body必须包含hello字段',
];
return $res;
}
//或者
if (!isset($arr['world'])) {
throw new ValidationException('body必须包含world字段');
}
return $res;
}
/**
* @Post(path="/test/upload", description="上传测试")
* @File(key="sec", rule="�默认为data字段")
* @ApiResponse(code=200, schema={"$ref":"Response"}, refValue="Company")
*/
public function company3() {
$data = self::getDefaultDataBySchemaName('Company');
return self::doSuccess($data);
}
}
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