Laravel-5.1----Integrate mews CAPTCHA into your project!

Source: Internet
Author: User
Tags cloudflare

After groping, finally can apply the verification code in Laravel 5.1.

Because the English slag five levels, so almost no search for anything useful, so consider on GitHub search verification Code pack!

Note: The package on GitHub often has instructions to use, as the above implementation, generally can be done.

I'm using mews captcha.

Git address: Https://github.com/mewebstudio/captcha above is used in detail.

Hands-on implementation:

--Manually enter the Laravel project directory

--in the corresponding directory, locate the Composer.json file, open it, and add the following statement:

Add statement

{"require": {...... "Mews/captcha": "~2.0"}, "minimum-stability": "Dev"}

--Perform composer update, if error, perform composer Self-update before performing composer update

--Find config/app.php open and add the following statement:

1. Open the file, locate the providers entry, and add the following statement

Mews\captcha\captchaserviceprovider::class,

2. Locate the aliases key and add the following statement:

To add a statement:

' Captcha ' = Mews\captcha\facades\captcha::class,

--cmd window to execute PHP artisan vendor:publish

Through all the steps above, you will find the directory under Vendor, and you can use the namespace using Captcha, a mews configuration file captcha.php that has a captcha in config/captcha.php.

--All the steps above are also present in Https://github.com/mewebstudio/captcha!

Let's take a look at the new Vendor/mews directory

We found vendor/mews/captcha/src/captcha.php.

Find the public method and find:

Public function Create ($VAR)---generate a verification code

Public function Check ($VAR)---Determine if the input and verification codes are equal

Public function src ($var)---output img attribute src link

Public function img ($VAR)---output img tag

Where the $var parameter is the corresponding key in the config/captcha.php, which can be:

Default, flat, Mini, inverse represents four styles of verification code! Can be configured for different styles, modify configuration items directly in config/captcha.php

--Example:

1. Configure Routing in app/http/routes.php

Where Captcha/test, which represents the test path, maps Captchacontroller under Public Function Index () {}

Captcha/mews mapping Verification Code output, mapping Captchacontroller under Public Function Mews () {}

2. Create a Controller

In the cmd window, enter directly

PHP Artisan Make:controller Captchacontroller--plain

If the prompt is successful, a new file is generated under app/http/controllers/captchacontroller.php

3. New index () and Mews () methods in app/http/controllers/captchacontroller.php

<?phpnamespace app\http\controllers;use app\http\requests;use app\http\controllers\controller;use Illuminate\ http\request;//references the corresponding namespace use Session;use Captcha;class Captchacontroller extends Controller {/** * Test page */Public Fu  Nction index () {//$mews = captcha::src (' inverse ');, compact (' mews ') return view (' Captcha.index ');  }/* Create verification code */Public Function Mews () {return captcha::create (' Default '); }}

4. Create a View

Create the Captcha directory under the resources/views/directory, creating a new index.blade.php and probably a layout and style adjustment

<! Doctype html> 

This time input: Http://your-host/captcha/test view

This is my layout and captcha.

5. Add route to app/http/routes.php

I submit to the background for testing via post

6. The CPT method in TestController is as follows:

<?phpnamespace App\Http\Controllers\Test;use Illuminate\Http\Request;use App\Http\Requests; use app\http\controllers\controller;use input, validator, redirect, session,  CAPTCHA;CLASS&NBSP;TESTCONTROLLER&NBSP;EXTENDS&NBSP;CONTROLLER{&NBSP;&NBSP;PUBLIC&NBSP;FUNCTION&NBSP;CPT ( request  $request)  {    // dd (Input::get (' CPT '));     $ rules = [       "CPT"  =>  ' Required|captcha '      ];     $messages  = [       ' cpt.required '  =>  ' Please enter the verification code ',       ' Cpt.captcha '  =>  ' Verification code error, please retry '      ];   //If you only verify the value of Captcha    //use  captcha::check (Input::get (' CPT '));  //return bool     $validator  = validator::make (Input::all (),  $rules,  $ messages);      if ($validator->fails ())  {      return redirect:: Back ()->witherrors ($validator);    } else {       return  "Verification Code ok!";     }  }}

Above, the test is as follows,

Validation error:

Verify correct:

There is no use of the Captcha::check () method, specific analysis of the situation.


Laravel-5.1----Integrate mews CAPTCHA into your project!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.