Download the PHP package jundayw/laravel-render-provider without Composer
On this page you can find all versions of the php package jundayw/laravel-render-provider. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jundayw/laravel-render-provider
More information about jundayw/laravel-render-provider
Files in jundayw/laravel-render-provider
Package laravel-render-provider
Short Description laravel render provider
License MIT
Homepage https://github.com/jundayw/laravel-render-provider
Informations about the package laravel-render-provider
Laravel Render Provider
Laravel 响应数据格式化服务提供者,它提供了一个链式、灵活、可扩展的响应数据构建工具,非常适合 API 返回场景。
主要特性
-
规范 API 响应格式: 适用于中大型 Laravel 应用中统一所有接口的响应结构,便于前后端协作与调试。
-
隐藏敏感字段而不丢失数据签名能力: 可通过
all(false)
保留隐藏字段进行签名再输出,避免签名丢失完整性。 - 扩展性强:
任意响应格式(如 XML、Markdown、HTML 预览)都可通过
macro
或重写response()
灵活实现。
安装方法
命令行下, 执行 composer 命令安装:
`
authentication package that is simple and enjoyable to use.
对象方法
替换键值
replace(string $oldKey, string $newKey): $this
隐藏键值
hide(mixed $hides): $this
移除键值
forget(mixed $forgets): $this
追加数据
with(string $key, mixed $value): $this
追加数据
withXxx($value): $this
重置对象
reset(): $this
刷新对象及宏
flush(): $this
批量赋值
data(array $data = [], bool $append = false): $this
获取所有数据
all(bool $hide = true): array
获取值
get(string $key): mixed
数据响应
response(?callable $response = null): mixed
JSON
json(?int $status = 200, ?array $headers = [], ?int $options = JSON_UNESCAPED_UNICODE): $this
JSONP
jsonp(?string $callback = 'jsonp', ?int $status = 200, ?array $headers = [], ?int $options = JSON_UNESCAPED_UNICODE): $this
宏:内置成功
success(?string $message = 'SUCCESS', ?string $url = null, mixed $data = null): $this
宏:内置失败
error(?string $error = 'ERROR', ?string $url = null, mixed $errors = null): $this
宏
macro($name, $macro): mixed
使用场景
开箱即用
替换键值
将响应数据中键值 timestamp 替换为 time
移除键值
若响应数据中键值 timestamp、url 不需要,可将其移除
追加数据
若响应数据中需要新增字段,可使用 with 方法
隐藏键值
若响应数据中需要对敏感数据进行处理,可使用 hide 方法
扩展签名
将响应数据及 appid、appkey 进行签名,并且响应数据中不显示 appkey 字段
响应场景
默认响应数据格式 json
响应数据格式 jsonp
扩展响应数据格式:宏方法扩展
扩展响应数据格式:response 方法扩展
宏场景
RenderFacade
应用包已扩展 success/error 方法,若不适用业务场景,可通过 Render::flush() 方法格式化后自行定义。
调用方式
ResponseFacade
调用方式
其他场景
批量赋值场景
链式操作优先级
为防止目标数据与预期数据不一致,推荐链式操作优先级: