Laravel BotScout
, (*1)
, (*2)
Protect your website against automated scripts using the botscout.com API., (*3)
Installation
You can install the package via composer:, (*4)
``` bash
composer require nicolasbeauvais/laravel-botscout, (*5)
Next, you must install the service provider:
```php
// config/app.php
'providers' => [
...
NicolasBeauvais\LaravelBotScout\BotScoutServiceProvider::class,
];
Add your botscout.com api key to the .env file:, (*6)
BOTSCOUT_SECRET=your-api-key
If needed you can also publish the config file:, (*7)
php artisan vendor:publish --provider="NicolasBeauvais\LaravelBotScout\BotScoutServiceProvider" --tag="config"
If you want to make use of the facade you must install it as well:, (*8)
// config/app.php
'aliases' => [
...
'BotScout' => NicolasBeauvais\LaravelBotScout\BotScoutFacade::class,
];
Usage
You are highly advised to read the BotScout.com API guide to understand the meaning of
each method., (*9)
Validator
You can easily use botscout in your existing validators:, (*10)
``` php
// Validate name
$validator = Validator::make(['name' => 'John Doe'], [
'name' => 'required|botscout_name'
]);, (*11)
// Validate email
$validator = Validator::make(['email' => 'toto@gmail.com'], [
'email' => 'required|botscout_mail'
]);, (*12)
// Validate ip
$validator = Validator::make(['ip' => '127.0.0.1'], [
'ip' => 'required|botscout_ip'
]);, (*13)
Note that you will need to create the validation message by yourself, as described in the [Laravel documentation](https://laravel.com/docs/5.5/validation#custom-error-messages).
### Facade
You can use the BotScout facade anywhere in your app:
```php
BotScout::multi('John Doe', 'email@test.com', '127.0.0.1')->isValid();
BotScout::all('John Doe')->isValid();
BotScout::name('John Doe')->isValid();
BotScout::mail('email@test.com')->isValid();
BotScout::ip('127.0.0.1')->isValid();
// We also include a quick way of testing a user with integrated exception catch
BotScout::check('John Doe', 'email@test.com', '127.0.0.1'); // true or false
Real life example using the check method
The check method is the recommended way to validate a register form:, (*14)
The check method is a wrapper to the multimethod that catch any http error / timeout. If the botscout api is not responding, the method will return false., (*15)
// Create a classic validation
$validator = Validator::make($request->all(), [
'email' => 'required|email|unique:users',
'name' => 'required|max:20',
]);
$validator->after(function ($validator) {
if (!BotScout::check($request->get('name'), $request->get('email'), $request->ip())) {
$validator->errors()->add('email', 'Sorry, it looks like your a bot!');
}
});
Changelog
Please see CHANGELOG for more information what has changed recently., (*16)
Testing
bash
$ composer test, (*17)
Contributing
Please see CONTRIBUTING for details., (*18)
Security
If you discover any security related issues, please email nicolasbeauvais1@gmail.com instead of using the issue tracker., (*19)
Credits
License
The MIT License (MIT). Please see License File for more information., (*20)