Usage
Basic controller
use Milenmk\LaravelBlacklist\BlacklistService;
class YourController
{
protected BlacklistService $blacklistService;
public function __construct(BlacklistService $blacklistService)
{
$this->blacklistService = $blacklistService;
}
public function store(Request $request)
{
// Validate request...
// Check fields against blacklisted words
$blacklistErrors = $this->blacklistService->checkFields([
'name' => $request->input('name'),
'email' => $request->input('email'),
// Add any other fields you want to check
]);
if (!empty($blacklistErrors)) {
return redirect()->back()->withErrors($blacklistErrors);
}
// Continue with your logic...
}
}
Livewire component
use Livewire\Component;
use Milenmk\LaravelBlacklist\BlacklistService;
class YourComponent extends Component
{
protected BlacklistService $blacklistService;
public function mount(): void
{
$this->blacklistService = app(BlacklistService::class);
}
Livewire form
use Livewire\Form;
use Milenmk\LaravelBlacklist\BlacklistService;
class YourForm extends Form
{
protected BlacklistService $blacklistService;
public function __construct($componentOrService = null, $propertyName = null)
{
parent::__construct($componentOrService, $propertyName);
$this->blacklistService = app(BlacklistService::class);
}
}
Advanced Usage
Blacklist Validation Rule
You can use the BlacklistRule in your form requests or validation logic.
use Milenmk\LaravelBlacklist\Rules\BlacklistRule;
// ...
public function rules(): array
{
return [
// Uses 'username' as the context (checks 'contexts.username' in config)
'username' => ['required', new BlacklistRule()],
// Explicitly specify the context
'bio' => ['required', new BlacklistRule('strict_bio')],
];
}
Route Middleware
Protect your routes using the blacklist middleware.
// Protect a route using the 'comment' context
Route::post('/comments', ...)->middleware('blacklist:comment');
// If no context is provided, it uses the field names from the request as contexts
Route::post('/profile', ...)->middleware('blacklist');
Whitelist and Ignore Patterns
You can define global exceptions in your configuration file:
- Whitelist: Exact words that should never be blocked (e.g., "Analyst").
- Ignore Patterns: Regex patterns to ignore (e.g., ignoring UUIDs or specific codes).
// config/blacklist.php
'whitelist' => ['Analyst', 'Dickson'],
'ignore_patterns' => ['/^TX-\d+$/'],
Advanced Matching Strategies
You can define custom lists with specific matching strategies in config/blacklist.php:
'lists' => [
'strict_list' => [
'terms' => ['forbidden'],
'matching' => 'exact',
],
'typo_list' => [
'terms' => ['important'],
'matching' => 'fuzzy', // Uses Levenshtein distance
'threshold' => 1, // Matches "1mportant"
],
'leet_list' => [
'terms' => ['hacker'],
'matching' => 'substitution', // Matches "h4ck3r"
],
],
Context-Aware Validation
Map different contexts (fields) to specific lists:
'contexts' => [
'username' => ['blacklist', 'strict_list'],
'comment' => ['profanity', 'typo_list', 'leet_list'],
],
Custom Log Channel
You can specify a custom log channel:
$blacklistErrors = $this->blacklistService->checkFields([
'name' => $request->input('name'),
'email' => $request->input('email'),
], 'security');
Switching Modes
You can change the filtering mode in your config file:
// config/blacklist.php
'mode' => 'blacklist', // Only check system blacklist words
// OR
'mode' => 'profanity', // Only check profanity/offensive words
// OR
'mode' => 'both', // Check both lists
The error messages will indicate which list the matched term belongs to:
- "The {field} contains the blacklisted word: "{term}""
- "The {field} contains the profanity word: "{term}""
Word Matching
This package uses whole word boundary matching to prevent false positives. For example:
- "admin" will match in "admin user" but not in "administrator" or "badminton"
- "damn" will match in "that's damn good" but not in "condamnation"
This ensures that legitimate content isn't incorrectly flagged while still catching problematic terms.
Enhanced Attribute Name Support (New)
The checkFields() method now accepts an optional third parameter $attributes — an associative array mapping field
names to human-readable labels:
$attributes = [
'last_name' => 'Last Name',
'name' => 'Name',
'email' => 'Email Address',
// Add your fields here
];
$blacklistErrors = $this->blacklistService->checkFields($input, null, $attributes);
This enables error messages to display friendly field names instead of raw input keys. For example:
The Last Name contains a blacklisted word: administrator
instead of
The last_name contains a blacklisted word: administrator
This ensures your error messages remain clear and consistent with Laravel's native validation attribute naming
conventions, improving user experience.