Download the PHP package zeaven/laravel-easy-suit without Composer

On this page you can find all versions of the php package zeaven/laravel-easy-suit. 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 laravel-easy-suit

Laravel Easy Suit

OSCS Status

这是一个为了方便使用Laravel框架开发api而提供的简单封装套件。集合了参数验证、统一返回格式、错误码定义、Sanctum和JWT、日志、代码生成。

Table of contents generated with markdown-toc

安装

发布

`

Postman 代码生成器

使用前先在.env文件添加postman的apiToken:

POSTMAN_API_TOKEN=xxx

然后执行 artisan pm:run

pm:run使用

postman接口定义如下:

postman接口定义

生成的控制器代码: controller

生成的Request代码: request

其他文件不一一展示,接口代码生成后,只需要配置参数验证规则,和在Logics目录编写业务逻辑代码即可

路由配置自动添加,但是中间件需要自行配置

同时需要修改RouteServiceProvider.php的路由代码如下:

你也可以按照configRoute方法的写法定义路由:

Request封装

BaseRequest继承于FormRequest,增加一个rule方法配置参数规则。

定义Request对象

Request对象提供两个方法获取参数:params和values,注意:未配置的参数是无法通过这两个方法获取的

Request对象参数配置

rule方法返回参数的配置,完整配置字段如下:

  1. rule 与Laravel的表单验证规则一致;
  2. default 默认值;
  3. type 参数类型,可选值有:int、float、bool、array(json)、date、ip、url、split(将字符串转以逗号分割成数组);
  4. as 别名,使用values()方法返回的key值;

全局返回统一格式

在使用前需要先添加路由中间件:

在easy_suit.php配置文件中有如下默认配置:

fields 指定返回的字段,以及字段名称,如果定义为false则不显示 exclude 可定义排除的路由

在控制器中 调用ok()全局方法,返回即可。

错误码和异常抛出

错误码定义

在error_code.php配置文件中定义错误码信息,下面的规则只是参考,至于你喜欢多少位的错误码,完全由你决定

错误码会发布到lang目录下,因为它是支持多语言的。

异常抛出

使用全局方法在你需要的地方抛出异常

所有异常抛出方法的最后一个参数可以传入一个数组,用于本地化参数替换,替换翻译字符串中的参数

注解日志

注解日志采用控制器方法添加注解的方式实现,在使用前需要先添加路由中间件:

日志默认使用laravel的日志服务,你可以在easy_suit.php配置文件中修改自定义的处理程序,以及是否开启日志

MyAnnoLogHandler 对象必须实现接口 \Zeaven\EasySuit\Annotations\AnnoLogHandler;

在控制器使用日志

当此方法有请求时,日志会在返回用户结果后保存

内置注解模板变量

在用户登录状态下,登录用户模型的缓存字段信息将自动添加到模板变量,可直接使用,如:

用户认证

配置

在easy_suit.php文件中

自动刷新Token

内置的Authenticate对Token验证的同时,如果token超过刷新周期,

则会自动刷新对应的Token,通过响应头下发给客户端,所以客户端应该在请求成功回调中,判断响应头是否包含“Authorization”字段,有的话,记得刷新本地Token。

`

注:自动刷新token必须在客户端请求中添加Authorization请求头

Sanctum认证

在配置文件中启用Sanctum认证后,将会使用内置的Authenticate接管认证过程,

同时为Sanctum认证增加自动刷新token功能,具体配置项如下;

如上配置,token将会在每小时刷新一次,每次有效期是两周 即两周内有访问,token有效期就可以一直往后延 每次刷新后,原来的token也可以选择是否需要删除

使用Sanctum认证

在路由配置中添加auth中间件

JWT认证

在配置文件中启用JWT认证后,将会使用内置的Authenticate接管认证过程,

同时为JWT认证增加自动刷新token功能。

安装JWT第三方包

Laravel 9.x 不支持 tymon/jwt-auth 包,但可以指定开发版

配置JWT

按照tymon/jwt-auth配置,在auth.config添加jwt的守卫配置后

在easy_suit.php配置文件中,也把auth.jwt.guard改成你添加的守卫名称,这里都是"jwt"

使用JWT认证

在路由配置中添加auth中间件

ResponseMapper 资源映射

对应的是Laravel的API 资源,使用的场景不多,所以采用配置的方式将数据转换成JSON格式。

Mapper生成

可以通过artisan命令生成Maaper文件

Mapper配置

mapper属性配置字段映射 hidden属性配置字段隐藏

假定当前数据模型如下:

希望返回的JSON格式如下:

对应的mapper配置:

使用

直接在控制器中返回

Model扩展

在easy_suit.php配置扩展开关

开启分页简化

Laravel默认分页对象返回的字段过多,开启简化后只返回两个字段"items"、"total"

开启扩展

为Laravel Model增加几个扩展方法

withs

Laravel 9.18 版本以上推荐直接使用官方的with方法。

withs 用于加载嵌套模型,假如有模型关系 A<-B<-C<-D。

selectWhen

如果指定字段数组不为空则使用,否则返回所有字段

whereWhen

如果指定过滤条件数组不为空则使用,常用在表格筛选提交的条件中

filledWhen

当给定参数有值时,执行回调

betweenWhen

likeWhen

当给定参数有值,则对指定字段模糊查询


All versions of laravel-easy-suit with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0.2
hidehalo/nanoid-php Version ^1.1
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 zeaven/laravel-easy-suit contains the following files

Loading the files please wait ....