Download the PHP package zero0719/hyperf-api without Composer

On this page you can find all versions of the php package zero0719/hyperf-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package hyperf-api

Feature

安装

发布配置文件

php bin/hyperf.php vendor:publish zero0719/hyperf-api

跨域处理

跨域处理我们只需要在响应的时候给 response header 做一定处理就行

修改配置文件中响应的 header 信息

config/autoload/api.php

在中间件配置文件 http 模块引入中间件

config/autoload/middlewares.php

统一响应

在大部分 api 中我们都是响应一个 json 格式的数据即可

我们约定响应的数据结构为

为什么 code 等于 0 的时候认为成功,因为这样可以和前端约定,只要是 0 都是成功,当 code 不为 0 的情况再特定处理;如果约定 1 为成功,0 为失败,当发生其他情况又把大于 1 当成失败去处理,理解逻辑上会有点奇怪。

可以选择继承 Zero0719\HyperfApi\Controller\BaseController

也可以直接引入 trait

验证器统一处理

当我们使用验证器类注入验证时,验证失败会抛出一个异常,框架提供的异常处理器不能满足我们 api 响应的格式,所以在这里我们要重写一个异常处理器,并响应成我们统一的数据格式。

config/autoload/exceptions.php

业务异常&处理器

在业务逻辑中,我们经常会有代码执行逻辑达到某个条件则认为不该继续往下执行了,这种可控的业务逻辑异常,我们统一的抛出异常,然后用统一的异常处理器捕捉处理。

异常类: Zero0719\HyperfApi\Exception\BusinessException

config/autoload/exceptions.php

模型未找到异常处理

当我们使用 Model::findOrFail($id) 时如果没有对应的模型,会抛出 ModelNotFoundException 错误,因为这个逻辑也比较常用,所以我们统一捕捉这个错误,响应消息为 资源未找到

exceptions.php

应用错误统一处理

有些未知的错误我们可能没有捕捉到,hyperf 的脚手架项目自带了 AppExceptionHandler.php,但是响应的结果并不是我们 api 项目想要的,所以在这之前进行了统一处理,响应服务出错的消息,并区分环境记录到日志当中。

exceptions.php

JWT 验证

json web token 几乎是开发 api 时离不开的一个模块,所以在这里直接把相关的业务嵌套进来

我们使用 phper666/jwt-auth 作为基础,详细用法请查看该包文档

阅读文档可知,用 Phper666\JWTAuth\Middleware\JWTAuthDefaultSceneMiddleware::class 中间件就可以进行 jwt 授权验证,如果没有通过校验则会抛出 Phper666\JWTAuth\Exception\JWTException 或者 Phper666\JWTAuth\Exception\TokenValidException,所以我们只需要定义异常处理器捕获这两个异常,并统一响应即可。

config/autoload/exceptions.php

抛出的异常中 400, 401 我们直接响应出去,可以和客户端约定当收到这两个码时可以尝试刷新 token 的逻辑,或者直接重定向到登录页面。

Zero0719\HyperApi\Service\TokenService.php 简单封装了 jwt 一些常用的业务,比如生成,销毁,刷新,解析等,对应我们在业务开发时,登录注册返回的 token,退出登录时销毁,刷新时销毁并重新生成,解析获取用户生成时的关键数据进行对应的业务处理

如果 TokenService 不满足你的业务场景,大可以继承以后重写,也可以完全重新写自己的业务类。

另外建议当某个地方的业务解析了此次请求的 token 以后,获取了关键用户信息,我们可以把这个信息用上下文 Context 存起来,方便这次请求其他业务还需要用时可以快速获得,这里逻辑请自行处理。

请求日志记录

在项目中,记录请求日志对于我们定位排查问题有很大的帮助,比如想知道用户用了什么客户端发起请求,用户发起请求的IP,时间,传了什么参数等等,我们可以定义一个中间件全局记录所有请求或者部分路由。

引入中间件 config/autoload/middlewares.php

修改配置文件 config/autoload/api.php

这里需要关注 handlerdata,因为在中间件中可以看到我们是需要实现了 RequestLogInterface 接口的类,而 data 中则定义了我们需要记录哪些数据,在对应的实现类中完成对应的方法,实现类的 handle 方法会获取 data 中的值,再去执行对应方法记录返回值。

如果 data 中某个方法的逻辑需要改变或者需要增加新的记录值,我们可以写自己的具体类并继承 Zero0719\HyperfApi\Service\RequestLogService::class

常用工具类

Zero0719\HyperfApi\Utils\CommonUtil

其他

中间件配置

jwt中间件建议配合路由组使用

异常处理器配置


All versions of hyperf-api with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
phper666/jwt-auth Version ~4.0.0
hyperf/validation Version ~3.1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zero0719/hyperf-api contains the following files

Loading the files please wait ....