mjarestad/filtry
Composer 安装命令:
composer require mjarestad/filtry
包简介
Filter and sanitize input data in Laravel 4/5 or as a standalone package.
README 文档
README
#Filtry
A package to filter and sanitize input data in Laravel 4/5 or as a standalone package. This is perfect to use with the Laravel 4 Validation class to filter data before validation. You can easily extend and create your own custom filters.
##Requirements php version > 5.6
##Installation
composer require mjarestad/filtry
Laravel
Add the ServiceProvider to the providers array in app/config/app.php
Laravel 5:
Mjarestad\Filtry\FiltryServiceProviderLaravel5::class,
Laravel 4:
'Mjarestad\Filtry\FiltryServiceProvider',
Add the Facade to the aliases array in app/config/app.php
'Filtry' => 'Mjarestad\Filtry\Facades\Filtry',
##Usage
###Laravel 5
####Form Requests
Extend your Form Request Validation classes with the provided Filtry Request to filter input data before validation.
<?php use Mjarestad\Filtry\Http\Requests\Request; class StorePostRequest extends Request { public function rules() { return [ 'author' => 'required', 'slug' => 'required', ]; } public function filters() { return [ 'author' => 'trim|ucwords', 'slug' => 'trim|replace:%,percent|slug', ]; } }
###Laravel 4
Add a the filters property to your Eloquent Model or anywhere else you prefer.
<?php class Post extends Eloquent { public static $filters = array( 'author' => 'trim|ucwords', 'slug' => 'trim|replace:%,percent|slug' ); public static $rules = array( 'author' => 'required', 'slug' => 'required' ); }
In your controller or service call Filtry::make() and provide the data to filter and your filters array.
<?php $filtry = Filtry::make(Input::all(), Post::$filters);
To get the filtered value use $filtry->getFiltered()
<?php $validator = Validator::make($filtry->getFiltered(), Post::$rules);
To get the unfiltered values, use:
<?php $filtry->getOld();
Every method can be used to filter a single value.
<?php Filtry::trim('some string'); Filtry::slug('some string'); Filtry::snakeCase('some string');
###Standalone
<?php $filters = [ 'author' => 'trim|ucwords', 'slug' => 'trim|replace:%,percent|slug', ]; $data = [ 'author' => 'John Doe', 'slug' => 'My post title', ]; $filtry = new Mjarestad\Filtry\Filtry; $filtry->make($data, $filters); $filteredData = $filtry->getFiltered();
##Create custom filters
###Laravel
Extend with custom filters to use in Filtry::make() or as dynamic methods.
<?php Filtry::extend('my_custom_filter', function ($data) { return str_replace('-', '_', $data); });
Call the extended filter dynamically
<?php Filtry::myCustomFilter('some-custom-string');
Optional parameters
You can define optional parameters for your filter.
<?php Filtry::extend('custom_filter', function ($data, $param1, $param2) { return $data . ($param1 + $param2); });
And then add the parameters in your request:
<?php use Mjarestad\Filtry\Http\Requests\Request; class StorePostRequest extends Request { public function rules() { return [ 'author' => 'required', ]; } public function filters() { return [ 'author' => 'custom_filter:1,2', ]; } }
It will concatenate 3 to your author attribute.
###Standalone
<?php $filtry = new Mjarestad\Filtry\Filtry; $filtry->extend('my_custom_filter', function ($data) { return str_replace('-', '_', $data); }); $filtry->myCustomFilter('some-custom-string');
##Available filters
###Core PHP filters
- trim
- ltrim
- rtrim
- lower (strtolower)
- upper (strtoupper)
- ucfirst
- ucwords
- stripslashes
- replace:search,replace (str_replace)
###Custom filters
- xss_clean - clean string with htmlspecialchars
- strip_whitespaces - strip all white spaces
- strip_dashes - strip all dashes
- slug - makes string url-friendly
- prep_url - adds http:// if not present
###Laravel filters
- snake_case
- camel_case
- studly_case
Theese filters can still be used in a non-Laravel application.
mjarestad/filtry 适用场景与选型建议
mjarestad/filtry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 221.56k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2013 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mjarestad/filtry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mjarestad/filtry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mjarestad/filtry 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
Symfony DataGridBundle
Data provider for yii2
This Laravel Nova package allows you to detach filters from the filter dropdown
Laravel filter elequent
Laravel filter elequent
统计信息
- 总下载量: 221.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-11-06