A Laravel package for filtering and sanitizing request variables with customizable rules
aporat/laravel-filter-var is a Laravel package for a laravel package for filtering and sanitizing request variables with customizable rules.
It currently has 2 GitHub stars and 2.067 downloads on Packagist (latest version v5.0.0).
Install it with composer require aporat/laravel-filter-var.
Discover more Laravel packages by aporat
or browse all Laravel packages to compare alternatives.
Last updated
A Laravel package for filtering and sanitizing request variables with a chainable, customizable filter system.
trim, uppercase, cast) in a single call.strip_tags, escape, format_date).Install the package via Composer:
composer require aporat/laravel-filter-var
The service provider (FilterVarServiceProvider) is automatically registered via Laravel’s package discovery. If you’ve disabled auto-discovery, add it manually to config/app.php:
'providers' => [
// ...
\Aporat\FilterVar\FilterVarServiceProvider::class,
],
Optionally, register the facade for cleaner syntax:
'aliases' => [
// ...
'FilterVar' => Aporat\FilterVar\Facades\FilterVar::class,
],
Publish the configuration file to customize filters:
php artisan vendor:publish --provider="Aporat\FilterVar\FilterVarServiceProvider" --tag="config"
This copies config/filter-var.php to your Laravel config directory.
Filter and sanitize a request variable using the facade:
use Aporat\FilterVar\Facades\FilterVar;
$userAgent = FilterVar::filterValue('cast:string|trim|strip_tags|escape', $request->header('User-Agent'));
This:
| Filter | Description | Example Input | Example Output |
|------------------------|----------------------------------------------------|---------------------------|------------------------|
| capitalize | Capitalizes words (title case) | hello world | Hello World |
| cast:<type> | Casts to a type (e.g., int, string, bool, array, object, collection) | 123.45 (cast:int) | 123 |
| digit | Extracts digits only | abc123xyz | 123 |
| escape | Escapes HTML special characters | <p>Hello &</p> | <p>Hello &</p> |
| filter_if | Conditional check on array key/value | ['key' => 'val'] | true/false |
| format_date | Reformats a date string | 2023-01-15 | 15/01/2023 |
| lowercase | Converts to lowercase | HELLO | hello |
| normal_string | Strips tags and keeps A-Z, 0-9, space, -:_. | <script>alert(1)</script> | alert1 |
| remove_whitespace | Removes all whitespace | a b c | abc |
| slugify | Converts string into URL-friendly slug | Hello World! | hello-world |
| strip_tags | Removes HTML/PHP tags | <b>Hello</b> | Hello |
| trim | Trims whitespace | hello | hello |
| uppercase | Converts to uppercase | hello | HELLO |
Chain multiple filters using the | separator:
$result = FilterVar::filterValue('trim|uppercase|cast:string', ' hello world ');
// Returns: "HELLO WORLD"
Add custom filters by editing config/filter-var.php:
return [
'custom_filters' => [
'media_real_id' => \App\Filters\MediaRealId::class,
],
];
Define the custom filter class:
namespace App\Filters;
use Aporat\FilterVar\Contracts\Filter;
class MediaRealId implements Filter
{
public function apply(mixed $value, array $options = []): string
{
$value = (string) $value;
return str_contains($value, '_') ? explode('_', $value, 2)[0] : $value;
}
}
Use it:
$result = FilterVar::filterValue('media_real_id', '11111_22222');
// Returns: "11111"
Resolve from the container:
$result = app('filter-var')->filterValue('trim', ' hello ');
// Returns: "hello"
Run the test suite:
composer test
Generate coverage reports for CI:
composer test-ci
Contributions are welcome! Please:
git checkout -b feature/amazing-feature).git commit -m "Add amazing feature").git push origin feature/amazing-feature).See CONTRIBUTING.md for details.
This package is open-sourced under the MIT License. See the License File for more information.