承接 andrey-vasin/laravel-request-filters 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

andrey-vasin/laravel-request-filters

Composer 安装命令:

composer require andrey-vasin/laravel-request-filters

包简介

Simple & lean request filtering for Laravel 5.6+

README 文档

README

Software License

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
  • FilterRequests trait that easily plugs into a FormRequest and enable filtering
  • InputFilter that allows developers to easily implement their own filters
  • RequestFiltering tool 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 andrey-vasin/laravel-request-filters

In your FormRequest use the following trait:

use AndreyVasin\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

MIT

统计信息

  • 总下载量: 714
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固