PHP code example of zero0719 / hyperf-api
1. Go to this page and download the library: Download zero0719/hyperf-api 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/ */
zero0719 / hyperf-api example snippets
return [
'cors' => [
'headers' => [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => true,
'Access-Control-Allow-Headers' => 'DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization'
]
]
];
return [
'http' => [
\Zero0719\HyperfApi\Middleware\CorsMiddleware::class
],
];
[
'code' => 0,
'message' => '1',
'data' => []
]
class IndexController extends \Zero0719\HyperfApi\Controller\BaseController
{
public function index() {
return $this->success([
'test' => 1
]);
}
}
use Zero0719\HyperfApi\Trait\ResponseTrait;
class Demo {
use ResponseTrait;
public function test() {
return $this->error('错误响应');
}
}
return [
'handler' => [
'http' => [
\Zero0719\HyperfApi\Exception\Handler\ValidationExceptionHandler::class,
.
.
.
],
],
];
return [
'handler' => [
'http' => [
\Zero0719\HyperfApi\Exception\Handler\BusinessExceptionHandler::class,
.
.
.
],
],
];
class IndexController extends \Zero0719\HyperfApi\Controller\BaseController
{
public function index()
{
$a = 1;
if ($a == 2) {
throw new \Zero0719\HyperfApi\Exception\BusinessException('业务逻辑有问题');
}
return $this->success();
}
}
return [
.
.
\Zero0719\HyperfApi\Exception\Handler\ModelNotFoundExceptionHandler::class,
.
]
return [
.
.
\Zero0719\HyperfApi\Exception\Handler\AppExceptionHandler::class,
.
]
return [
'handler' => [
'http' => [
.,
\Zero0719\HyperfApi\Exception\Handler\JWTExceptionHandler::class,
.,
.,
.
],
],
];
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Phper666\JWTAuth\Util\JWTUtil;
use Zero0719\HyperfApi\Controller\BaseController;
use Zero0719\HyperfApi\Service\TokenService;
class SessionsController extends BaseController
{
protected TokenService $tokenService;
public function __construct(TokenService $tokenService)
{
$this->tokenService = $tokenService;
}
public function login(RequestInterface $request)
{
// 进行了一大堆验证,获取到用户的关键信息,比如userId, username 等
$user = [
'userId' => 1,
'username' => 'test111'
];
$result = $this->tokenService->generate($user);
return $this->success($result);
}
public function logout(RequestInterface $request)
{
$token = JWTUtil::getToken($request);
$this->tokenService->destroy($token);
return $this->success();
}
public function reLogin(RequestInterface $request)
{
$token = JWTUtil::getToken($request);
return $this->success($this->tokenService->refresh($token));
}
public function me(RequestInterface $request)
{
$token = JWTUtil::getToken($request);
$data = $this->tokenService->parse($token);
return $this->success($data);
}
}
return [
'http' => [
.
\Zero0719\HyperfApi\Middleware\RequestLogMiddleware::class,
.
]
];
return [
'request' => [
'log' => [
'handler' => Zero0719\HyperfApi\Service\RequestLogService::class,
'data' => ['ip', 'method', 'url', 'param', 'time'],
'meta' => [],
'config' => env('REQUEST_LOG_CONFIG', 'default'),
'channel' => env('REQUEST_LOG_CHANNEL', 'request'),
'level' => env('REQUEST_LOG_LEVEL', 'info'),
]
]
];
'handler' => \App\Service\TestRequestInfo::class,
'data' => ['ip', 'method', 'url', 'param', 'time', 'ua'],
declare(strict_types = 1);
namespace App\Service;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Contract\RequestInterface;
use Zero0719\HyperfApi\Service\RequestLogService;
class TestRequestInfo extends RequestLogService
{
public function ua()
{
return ApplicationContext::getContainer()->get(RequestInterface::class)->getHeader('User-Agent');
}
}
// 获取客户端IP
\Zero0719\HyperfApi\Utils\CommonUtil::getIp();
return [
'http' => [
\Zero0719\HyperfApi\Middleware\CorsMiddleware::class,
\Zero0719\HyperfApi\Middleware\RequestLogMiddleware::class,
\Hyperf\Validation\Middleware\ValidationMiddleware::class
],
];
return [
'handler' => [
'http' => [
\Zero0719\HyperfApi\Exception\Handler\ValidationExceptionHandler::class,
\Zero0719\HyperfApi\Exception\Handler\JWTExceptionHandler::class,
\Zero0719\HyperfApi\Exception\Handler\ModelNotFoundExceptionHandler::class,
\Zero0719\HyperfApi\Exception\Handler\BusinessExceptionHandler::class,
\Zero0719\HyperfApi\Exception\Handler\AppExceptionHandler::class,
Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
App\Exception\Handler\AppExceptionHandler::class,
],
],
];