PHPackages                             aliziodev/laravel-username-guards - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. aliziodev/laravel-username-guards

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

aliziodev/laravel-username-guards
=================================

A comprehensive username filtering package for Laravel that helps filter profanity, adult content, illegal activities, gambling, spam, and religion abuse words

v1.0.1(1y ago)14762MITPHPPHP ^8.1|^8.2

Since May 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/aliziodev/laravel-username-guards)[ Packagist](https://packagist.org/packages/aliziodev/laravel-username-guards)[ RSS](/packages/aliziodev-laravel-username-guards/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (9)Versions (3)Used By (0)

Laravel Username Guards
=======================

[](#laravel-username-guards)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1e5a9eb4cf35ef637c2bfaf4e500babfdfb3368bcc8fa8ecd2825f7d0ec29c13/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c697a696f6465762f6c61726176656c2d757365726e616d652d6775617264732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aliziodev/laravel-username-guards)[![Total Downloads](https://camo.githubusercontent.com/5f1fa5f60846c822cb47c4e565610ec9c58cbfb01a2f0470fb23473a687e33fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c697a696f6465762f6c61726176656c2d757365726e616d652d6775617264732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aliziodev/laravel-username-guards)[![PHP Version](https://camo.githubusercontent.com/cfdce57f43e3cc66ff0763c06a651ab9e0a03fa61a24cc458b33e45df43af744/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f616c697a696f6465762f6c61726176656c2d757365726e616d652d6775617264732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aliziodev/laravel-username-guards)[![Laravel Version](https://camo.githubusercontent.com/095f07b46a36ffda6a1e41286b12a0e7cee7048f7762565692d3661d08bd34fe/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31302e782d7265643f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aliziodev/laravel-username-guards)[![Laravel Version](https://camo.githubusercontent.com/b0ed3341bf03bc1527750f5897b2f21d0fd01815215e74be4d7b17fa534637bf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31312e782d7265643f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aliziodev/laravel-username-guards)[![Laravel Version](https://camo.githubusercontent.com/e2ab74c900c407c3d7b80cd3689bb5c8f2767230a906c7b2c1e6f0ff8da66157/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31322e782d7265643f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aliziodev/laravel-username-guards)

Laravel Username Guards is a comprehensive package for validating usernames in Laravel applications. It provides robust validation features including pattern matching and prohibited word filtering across multiple languages.

Features
--------

[](#features)

- Pattern-based username validation (length, allowed characters, format)
- Prohibited word filtering in multiple categories (profanity, adult, spam, etc.)
- Multi-language support (en, id, and extendable)
- Flexible configuration
- Easy integration with Laravel Validation
- Facade for direct usage
- Caching for better performance

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require aliziodev/laravel-username-guards
```

Console Commands
----------------

[](#console-commands)

```
php artisan username-guard:install
```

This command:

- Publishes configuration file
- Publishes word resources
- Sets up basic configuration

Cache Clearing Command:

```
php artisan username-guard:clear
```

This command clears all caches related to the package:

- Configuration cache
- Application cache
- Package discovery cache

Configuration
-------------

[](#configuration)

After publishing the configuration file, you can customize various options in config/username-guard.php :

### Language Settings

[](#language-settings)

```
// Default language
'default_locale' => env('APP_LOCALE', 'en'),

// Supported languages
'supported_locales' => [
'global', // Global words (REQUIRED, applies to all languages)
'en', // English
'id', // Indonesian
// Add other languages as needed
],
```

### Filtering Mode

[](#filtering-mode)

```
// Check all supported languages
'check_all_locales' => env('WORD_FILTER_CHECK_ALL_LOCALES', true),

// Only use default language + global
'preferred_locale_only' => env('WORD_FILTER_PREFERRED_LOCALE_ONLY', false),
```

### Word Categories

[](#word-categories)

```
'categories' => [
// Default categories
'profanity' => true,
'adult' => true,
'gambling' => true,
'religion_abuse' => true,
'illegal' => true,
'spam' => true,
'reserved' => true,
'hate' => true,
'scam' => true,

    // Optional categories
    'political'      => false,
    'trending'       => false,

    // Custom category example
    // 'trademark' => true,

],
```

### Validation Patterns

[](#validation-patterns)

```
'patterns' => [
// Character sets
'sets' => [
'alpha' => 'a-zA-Z',
'numeric' => '0-9',
'special' => '_-',
'extra' => '.',
'spaces' => '\s',
],

    // Common validation rules
    'rules' => [
        'start_alpha' => '/^[a-zA-Z]/',                 // Must start with letter
        'end_alphanumeric' => '/[a-z0-9]$/',            // Must end with letter/number
        'no_consecutive_dash' => '/[-]{2,}/',           // No consecutive dashes
        'no_consecutive_underscore' => '/[_]{2,}/',     // No consecutive underscores
        // ...
    ],

    // Pattern presets
    'presets' => [
        'username' => [
            'allowed_chars' => '[^a-zA-Z0-9_-]',
            'rules' => ['start_alpha', 'no_consecutive_dash', 'no_consecutive_underscore', 'no_consecutive_special_mix'],
            'min_length' => 3,
            'max_length' => 20,
        ],
        // ...
    ],

    // Active patterns
    'active' => [
        'username' => true,      // Enable username pattern
        // ...
    ],

],
```

### Cache Settings

[](#cache-settings)

```
'cache' => [
'enabled' => env('WORD_FILTER_CACHE_ENABLED', true), // Enabled by default
'ttl' => env('WORD_FILTER_CACHE_TTL', 86400), // 24 hours
'store' => env('WORD_FILTER_CACHE_STORE', null), // Cache store: file, redis, etc
],
```

Usage
-----

[](#usage)

### Using Validation Rule

[](#using-validation-rule)

The easiest way to use this package is with the UsernameRule in Laravel validation:

```
