guerrilla/laravel-request-filters
Composer 安装命令:
composer require guerrilla/laravel-request-filters
包简介
Simple & lean request filtering for Laravel 5.6+
README 文档
README
About
Laravel provides tools to validate HTTP requests allowing developers to ensure the input data is in the correct structure.
This package provides tools to filter the valid data into the format intended.
It feels like it is part of the Laravel framework and couldn't be any simpler to use.
Requirements
Laravel 5.6+
Features
- Format input with a collection of pre-made and tested Filters
FilterRequeststrait that easily plugs into aFormRequestand enable filteringInputFilterthat allows developers to easily implement their own filtersRequestFilteringtool that can apply the same filters to any string you pass in- Nested AND array filtering just like Laravel's own validator 👌
- String based filtering similar to Laravel's validator + custom parsable filters
Included Filters
| Filter class | Usage |
|---|---|
| FilterCapitalize | Capitalizes the first character of each word |
| FilterSanitize | Escapes characters based on php's own validator constants |
| FilterSanitizeEmail | Sanitizes email |
| FilterSanitizeText | Sanitizes text generically |
| FilterSanitizeEncoded | URL Encodes text |
| FilterNumeric | Removes all non-numerical characters |
| FilterStripTag | Removes HTML and PHP tags, keeps what you want |
| FilterToLower | Converts to lowercase |
| FilterToUpper | Converts to uppercase |
| FilterTrim | Trim leading and trailing white space |
| FilterDate | Format into a specified Carbon date string see Carbon docs |
Included Filters (as string literals)
| Filter class | Usage |
|---|---|
| 'capitalize' | Capitalizes the first character of each word |
| 'email' | Sanitizes email |
| 'sanitize' | Sanitizes text generically |
| 'encode' | URL Encodes text |
| 'number' | Removes all non-numerical characters |
| 'strip' | Removes HTML and PHP tags, keeps what you want |
| 'lowercase' | Converts to lowercase |
| 'uppercase' | Converts to uppercase |
| 'trim' | Trim leading and trailing white space |
| 'date' | Format into a specified Carbon date string see Carbon docs |
You can make your own custom filters by implementing the FilterInterface :)
How to use
Import via composer:
composer require guerrilla/laravel-request-filters
In your FormRequest use the following trait:
use Guerrilla\RequestFilters\FilterRequests
Describe your filters (Laravel rules included for familiarisation):
public function rules():array { return [ 'email' => ['required', 'email', 'bail'], 'name' => ['required'], 'employees.*.name' =>['required'] ]; }
public function filters():array { return [ 'email' => [new FilterTrim, new FilterSanitizeEmail], 'name' => [new FilterTrim, new FilterSanitizeText, new FilterCapitalize], 'employees.*.name' => [new FilterCapitalize], 'date' => [new FilterTrim, new FilterDate('d/m/Y')] ]; }
Or use the string based syntax:
public function filters():array { return [ 'email' => 'trim|email', 'name' => 'trim|sanitize|capitalize', 'employees.*.name' => 'capitalize', 'date' => 'trim|date:d/m/Y' ]; }
Validate the request as per normal but, the results will be now filtered :)
$input = $request->validated(); echo $input['email']; //trimmed and sanitized email!
You can optionally just run the filter on any string you like outside of the request:
$validated_input = $request->validate([...]); $filtered_result = InputFilter::filter( $validated_input, [ 'email' => [new FilterTrim], 'name' => [new FilterTrim], 'meta.*.attributes' => [new MyCustom1Filter(1), new MyCustom2Filter(2)] ] );
Using your own custom filtering rules to the string parsing syntax is easy!
$validated_input = $request->validate([...]); $custom_filters = [ 'custom1' => MyCustom1Filter::class, 'custom2' => MyCustom2Filter::class ]; $filtered_result = InputFilter::filterFromString( $validated_input, [ 'email' => 'trim', 'name' => 'trim', 'meta.*.attributes' => 'custom1:1|custom2:2' ], $custom_filters );
License
guerrilla/laravel-request-filters 适用场景与选型建议
guerrilla/laravel-request-filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 906 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 09 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「filter」 「utility」 「request」 「trait」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 guerrilla/laravel-request-filters 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 guerrilla/laravel-request-filters 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 guerrilla/laravel-request-filters 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
Symfony DataGridBundle
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Data provider for yii2
This Laravel Nova package allows you to detach filters from the filter dropdown
统计信息
- 总下载量: 906
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-13