承接 mjarestad/filtry 相关项目开发

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

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

mjarestad/filtry

最新稳定版本:v1.1.3

Composer 安装命令:

composer require mjarestad/filtry

包简介

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

README 文档

README

#Filtry

Latest Stable Version Build Status License

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.

统计信息

  • 总下载量: 209.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固