# hyperf-template **Repository Path**: L_ing1992/hyperf-template ## Basic Information - **Project Name**: hyperf-template - **Description**: hyperf-template - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-11-15 - **Last Updated**: 2022-07-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # **Hyperf** 介绍 > Hyperf 是一个高性能、高灵活性的渐进式 PHP 协程框架,内置协程服务器及大量常用的组件,性能较传统基于 PHP-FPM 的框架有质的提升,提供超高性能的同时,也保持着极其灵活的可扩展性,标准组件均基于 PSR 标准 实现,基于强大的依赖注入设计,保证了绝大部分组件或类都是 可替换 与 可复用 的。 小版本更新 ```shell composer update "hyperf/*" -o ``` ## 修改内容: 0. [x] 端口配置 配置文件 _.env_ 中的 APP_PORT ``` APP_PORT=9501 ``` 1. [x] 日志监听开关 配置文件 _.env_ 中的 DB_LOG ``` DB_LOG=false ``` 2. [x] 日志路径配置 配置文件 _.env_ 中的 LOG_PATH ``` LOG_PATH=/home/hyperf-logs/${APP_NAME} ``` 3. [x] 生成 supervisor 配置文件 ```shell php bin/hyperf.php supervisor:create-ini php bin/hyperf.php supervisor:create-ini /home/hyperf-www/test/ ``` ``` mv app_name.ini /etc/supervisord.d/ supervisorctl update supervisorctl restart app_name ``` 4. [x] 自定义业务异常 BusinessExceptionHandler.php ```php namespace App\Exception\Handler; use App\Exception\BusinessException; use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\HttpMessage\Stream\SwooleStream; use Psr\Http\Message\ResponseInterface; use Throwable; class BusinessExceptionHandler extends ExceptionHandler { /** * @param Throwable $throwable * @param ResponseInterface $response * @return ResponseInterface */ public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface { // 判断被捕获到的异常是希望被捕获的异常 if ($throwable instanceof BusinessException) { // 阻止异常冒泡 $this->stopPropagation(); return $response->withStatus($throwable->getCode())->withBody(new SwooleStream($throwable->getMessage())); } // 交给下一个异常处理器 return $response; // 或者不做处理直接屏蔽异常 } public function isValid(Throwable $throwable): bool { return true; } } ``` BusinessException.php ```php namespace App\Exception; use Hyperf\Server\Exception\ServerException; use Throwable; class BusinessException extends ServerException { /** * @param string $message * @param int $code * @param Throwable|null $previous */ public function __construct(string $message = 'err', $code = 500, Throwable $previous = null) { parent::__construct($message, $code, $previous); } } ``` 使用 ```php throw new BusinessException; ``` 5. [x] 错误日志重定向 6. [x] 请求信息记录到日志 url method . ```php return [ 'http' => [ \App\Middleware\RequestMiddleware::class ], ]; ``` 7. [x] 指令 make:csm ```shell php bin/hyperf.php make:csm Admin/Test ```