ads

laravel bad word filter

laravel bad word filter

Laravel Check Bad Words


hi, my friends today in this tutorial I am explaining how to stop adding abuse words in laravel frameworks. Nowadays lots of problems will create if our site filled with comments used to the bad word or you can say abuse word.

This abuse word creates a problem in the frontend site website it means which pages the viewer can comment or write about something for review work or in support where you provide full support (chat system into your website).

Do not worry about that all types of problems I recently found about that package which already provided by the laravel package and the name is Laravel Profanity Filter. This package will solve your problem and it will help you to fix the bad words.

This package you can apply with your Laravel Validation rules and make rules as uniquely and just called that rule here into the validation rule so you can easily filter the bad word from laravel and no one commented on your website. This package will stop the request of the form before it fully saved the data on your website.

so let's begin for how to filter abuse word in laravel

Step:1

Step:2 Register in config/app.php

Register the service providers to enable the package:
Askedio\Laravel5ProfanityFilter\Providers\ProfanityFilterServiceProvider::class,


Step:3 run the command below

php artisan vendor:publish


let's take example for understand this profane package use into laravel framework.

Step:4 first create rule for returning proper data with check valid condition when storing the data

create validate.php file into app/Rules/.php

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class ValidateTag implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if ($value != strip_tags($value)) {
return false;
}

return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('validation.validate_tag');
}
}

Step:5 use this ValidateTage into the controller like below
use App\Rules\ValidateTag;

Step:6 create a first route for the contacts page (here I am taking the contact page example to understand this profane use into laravel)

Route::get('contact-us', 'ContactController@getContactUs')->name('contactUs');
Route::post('contact-us', 'ContactController@postContact');

Step:7 create migration for contact page
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateContactsTable151020 extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->string('email')->nullable();
$table->string('phone')->nullable();
$table->string('subject')->nullable();
$table->text('message')->nullable();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contacts');
}
}

Step:8 Create Model App/Models/Contact.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Contact extends Model
{
use SoftDeletes;

protected $fillable = [
'id', 'name', 'email', 'phone', 'subject', 'message', 'created_at', 'updated_at', 'deleted_at'
];
}

Step:9 create controller app/Http/Controller/ContactController.php
<?php

namespace App\Http\Controllers;

use App\Models\Contact;
use App\Rules\ValidatePhoneNumber;
use App\Rules\ValidateTag;
use Illuminate\Http\Request;

class ContactController extends Controller
{
public function getContactUs()
{
return view('pages.contact');
}

public function postContact(Request $request)
{
$validated = $this->validate($request, [
'name' => ['required', 'string', new ValidateTag()],
'email' => ['required', 'string', 'email', new ValidateTag()],
'phone' => ['required', new ValidateTag(), new ValidatePhoneNumber()],
'subject' => ['required', 'profane', new ValidateTag()],
'message' => ['required', 'profane', new ValidateTag()],
]);

$entity = Contact::create($validated);

if ($entity) {
return redirect('contact-us')->with('success', 'Successfully send your request.');
} else {
return redirect('contact-us')->with('error', 'Something went wrong, try again.');
}

}
}

Step:10 create blade file resource/views/pages/contact.blade.php

<!doctype html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="container-fluid">
<div class="container shop-contents">
<div class="row">
<div class="col-sm-12">
<div class="content-title">
<h1>Contact Us by <a href="https://www.mtitsolutions.in/">MTItsolutions</a> </h1>
</div>
</div>
</div>
</div>
</div>
<section>
<div class="container contact-info">
<div class="row">
<div class="col-sm-6 write-us-form">
<h3>Write Us Now</h3>
<div class="container">
<form method="POST" action="{{url('contact-us')}}" novalidate>
@csrf
<div class="row">
<div class="col-sm-12">
@if(session()->has('success'))
<div class="alert alert-success"><h5>{{session()->get('success')}}</h5></div>
@endif
@if(session()->has('error'))
<div class="alert alert-danger"><h5>{{session()->get('error')}}</h5></div>
@endif
</div>
<div class="col-sm-6">
<label for="uname"> Name</label>
<input type="text" name="name" required>
<div>
@if($errors->has('name'))
<span class="help-block text-danger"
role="alert">{{ $errors->first('name') }}</span>
@endif
</div>
</div>
<div class="col-sm-6">
<label for="uname">E-mail</label>
<input type="text" name="email" required>
<div>
@if($errors->has('email'))
<span class="help-block text-danger"
role="alert">{{ $errors->first('email') }}</span>
@endif
</div>
</div>
</div>
<div class="row">
<div class=col-sm-6>
<label for="psw">Phone</label>
<input type="text" name="phone" required>
<div>
@if($errors->has('phone'))
<span class="help-block text-danger"
role="alert">{{ $errors->first('phone') }}</span>
@endif
</div>
</div>
<div class=col-sm-6>
<label for="psw">subject</label>
<input type="text" name="subject" required>
<div>
@if($errors->has('subject'))
<span class="help-block text-danger"
role="alert">{{ $errors->first('subject') }}</span>
@endif
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-12 control-label" for="message">Your message</label>
<textarea class="form-control" id="message" name="message" rows="5"></textarea>
<div>
@if($errors->has('message'))
<span class="help-block text-danger"
role="alert">{{ $errors->first('message') }}</span>
@endif
</div>
</div>
<button type="submit">Submit</button>
</form>
</div>
</div>
</div>
</div>
</section>
</body></html> 
Here I used $errors for fetching validation error message on the frontend for particular fields.also cover success and error message for want to know more about success and error message you click on below link and know more about how to print errors and success message on blade via the controller.


 


Profanity Filter with PHP

Step:1 take an example like below as mentioned

use Askedio\Laravel5ProfanityFilter\ProfanityFilter;

$config = []; // Data from `resources/config/profanity.php`
$badWordsArray = []; // Data from `resources/lang/[lang]/profanity.php`

$profanityFilter =  new ProfanityFilter($config, $badWordsArray);
$string = $profanityFilter->filter('something with a bad word');




That's it. 
You can try on your laravel project and comment here if you have any problem so I can help you.

Thank you :)

Post a Comment

أحدث أقدم