Commit 2478d98a authored by Nguyen Van Anh's avatar Nguyen Van Anh Committed by GitHub

Merge pull request #90 from mmghv/readme

Update READMME.md
parents 52ebe72b cc5d4f50
No CAPTCHA reCAPTCHA [![Build Status](https://travis-ci.org/anhskohbo/no-captcha.svg?branch=master&style=flat-square)](https://travis-ci.org/anhskohbo/no-captcha) No CAPTCHA reCAPTCHA
========== ==========
[![Build Status](https://travis-ci.org/anhskohbo/no-captcha.svg?branch=master&style=flat-square)](https://travis-ci.org/anhskohbo/no-captcha)
[![Latest Stable Version](https://poser.pugx.org/anhskohbo/no-captcha/v/stable)](https://packagist.org/packages/anhskohbo/no-captcha)
[![Total Downloads](https://poser.pugx.org/anhskohbo/no-captcha/downloads)](https://packagist.org/packages/anhskohbo/no-captcha)
[![Latest Unstable Version](https://poser.pugx.org/anhskohbo/no-captcha/v/unstable)](https://packagist.org/packages/anhskohbo/no-captcha)
[![License](https://poser.pugx.org/anhskohbo/no-captcha/license)](https://packagist.org/packages/anhskohbo/no-captcha)
![recaptcha_anchor 2x](https://cloud.githubusercontent.com/assets/1529454/5291635/1c426412-7b88-11e4-8d16-46161a081ece.gif) ![recaptcha_anchor 2x](https://cloud.githubusercontent.com/assets/1529454/5291635/1c426412-7b88-11e4-8d16-46161a081ece.gif)
> For Laravel 4 use [v1](https://github.com/anhskohbo/no-captcha/tree/v1) branch. > For Laravel 4 use [v1](https://github.com/anhskohbo/no-captcha/tree/v1) branch.
...@@ -15,70 +21,109 @@ composer require anhskohbo/no-captcha ...@@ -15,70 +21,109 @@ composer require anhskohbo/no-captcha
### Setup ### Setup
Add ServiceProvider to the providers array in `app/config/app.php`. **_NOTE_** This package supports the auto-discovery feature of Laravel 5.5, So skip these `Setup` instructions if you're using Laravel 5.5.
``` In `app/config/app.php` add the following :
1- The ServiceProvider to the providers array :
```php
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class, Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class,
``` ```
2- The class alias to the aliases array :
```php
'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
```
### Configuration ### Configuration
Add `NOCAPTCHA_SECRET` and `NOCAPTCHA_SITEKEY` in **.env** file (without brackets): Add `NOCAPTCHA_SECRET` and `NOCAPTCHA_SITEKEY` in **.env** file :
``` ```
NOCAPTCHA_SECRET=secret-key NOCAPTCHA_SECRET=secret-key
NOCAPTCHA_SITEKEY=site-key NOCAPTCHA_SITEKEY=site-key
``` ```
(You can obtain them from [here](https://www.google.com/recaptcha/admin))
### Usage ### Usage
##### Init js source #### Init js source
``` With default options :
{!! app('captcha')->renderJs($lang = 'en', $callback = false, $onLoadClass = 'recaptchaCallback') !!}
```php
{!! NoCaptcha::renderJs() !!}
``` ```
##### Display reCAPTCHA With [language support](https://developers.google.com/recaptcha/docs/language) or [onloadCallback](https://developers.google.com/recaptcha/docs/display#explicit_render) option :
```php ```php
{!! app('captcha')->display(); !!} {!! NoCaptcha::renderJs('fr', true, 'recaptchaCallback') !!}
``` ```
With custom attributes and language support: #### Display reCAPTCHA
Default widget :
```php
{!! NoCaptcha::display() !!}
``` ```
{!! app('captcha')->display($attributes = []); !!}
With [custom attributes](https://developers.google.com/recaptcha/docs/display#render_param) (theme, size, callback ...) :
```php
{!! NoCaptcha::display(['data-theme' => 'dark']) !!}
``` ```
##### Validation #### Validation
Add `'g-recaptcha-response' => 'required|captcha'` to rules array. Add `'g-recaptcha-response' => 'required|captcha'` to rules array :
```php ```php
$validate = Validator::make(Input::all(), [ $validate = Validator::make(Input::all(), [
'g-recaptcha-response' => 'required|captcha' 'g-recaptcha-response' => 'required|captcha'
]); ]);
``` ```
### Testing ##### Custom Validation Message
When using the [Laravel Testing functionality](http://laravel.com/docs/5.1/testing), you will need to mock out the response for the captcha form element. To do this: Add the following values to the `custom` array in the `validation` language file :
1) Setup NoCaptcha facade in config/app.conf ```php
'custom' => [
'g-recaptcha-response' => [
'required' => 'Please verify that you are not a robot.',
'captcha' => 'Captcha error! try again later or contact site admin.',
],
],
```
Then check for captcha errors in the `Form` :
```php ```php
'NoCaptcha' => 'Anhskohbo\NoCaptcha\Facades\NoCaptcha' @if ($errors->has('g-recaptcha-response'))
<span class="help-block">
<strong>{{ $errors->first('g-recaptcha-response') }}</strong>
</span>
@endif
``` ```
2) For any form tests involving the captcha, you can then mock the facade behaviour: ### Testing
When using the [Laravel Testing functionality](http://laravel.com/docs/5.5/testing), you will need to mock out the response for the captcha form element.
So for any form tests involving the captcha, you can do this by mocking the facade behavior:
```php ```php
// prevent validation error on captcha // prevent validation error on captcha
NoCaptcha::shouldReceive('verifyResponse') NoCaptcha::shouldReceive('verifyResponse')
->once() ->once()
->andReturn(true); ->andReturn(true);
// provide hidden input for your 'required' validation // provide hidden input for your 'required' validation
NoCaptcha::shouldReceive('display') NoCaptcha::shouldReceive('display')
->zeroOrMoreTimes() ->zeroOrMoreTimes()
...@@ -96,11 +141,11 @@ Checkout example below: ...@@ -96,11 +141,11 @@ Checkout example below:
require_once "vendor/autoload.php"; require_once "vendor/autoload.php";
$secret = ''; $secret = 'CAPTCHA-SECRET';
$sitekey = ''; $sitekey = 'CAPTCHA-SITEKEY';
$captcha = new \Anhskohbo\NoCaptcha\NoCaptcha($secret, $sitekey); $captcha = new \Anhskohbo\NoCaptcha\NoCaptcha($secret, $sitekey);
if ( ! empty($_POST)) { if (! empty($_POST)) {
var_dump($captcha->verifyResponse($_POST['g-recaptcha-response'])); var_dump($captcha->verifyResponse($_POST['g-recaptcha-response']));
exit(); exit();
} }
...@@ -112,6 +157,7 @@ if ( ! empty($_POST)) { ...@@ -112,6 +157,7 @@ if ( ! empty($_POST)) {
<button type="submit">Submit</button> <button type="submit">Submit</button>
</form> </form>
<?php echo $captcha->renderJs(); ?>
``` ```
## Contribute ## Contribute
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment