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; Useapp\http\requests; UseApp\http\controllers\controller; Useilluminate\http\request;//referencing the corresponding namespace UseSession; UseCaptcha;classCaptchacontrollerextendsController {/** * Test page*/     Public functionindex () {//$mews = captcha::src (' inverse ');, compact (' mews ')        returnView (' Captcha.index '); }/* Create verification code * / Public functionMews () {returnCaptcha::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><HTMLLang= "en"><Head>    <MetaCharSet= "Utf-8">    <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">    <Metaname= "Viewport"content= "Width=device-width, initial-scale=1">    <title>Learn Laravel 5.1</title>    <Linkhref= "{{Asset ('/css/app.css ')}}"rel= "stylesheet">    <!--Fonts -    <Linkhref= '//fonts.useso.com/css?family=roboto:400,300 'rel= ' stylesheet 'type= ' Text/css '>    <!--HTML5 Shim and Respond.js for IE8 support of HTML5 elements and media queries -    <!--WARNING:Respond.js doesn ' t work if you view the page via file:// -    <!--[If Lt IE 9]> <script src= "Https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js" ></script> <script src= "Https://oss.maxcdn.com/respond/1.4.2/respond.min.js" ></script> <! [EndIf] -</Head><Body>    <H2>This page is used only to learn the verification code</H2>    <H3>Mews</H3>@if (Count ($errors) > 0)<Divclass= "Alert Alert-danger">        <ul>@foreach ($errors->all () as $error)<Li>{{$error}}</Li>@endforeach</ul>    </Div>@endif<formAction= "{{URL (' Test/cpt ')}}"Method= "POST">        <inputtype= "hidden"value= "POST"name= "_method"><!--      -<inputtype= "hidden"value= "{{Csrf_token ()}}"name= "_token" /><!--      -<inputtype= "text"name= "CPT"value="" /><!--      -<imgsrc= "{{URL (' Captcha/mews ')}}"onclick= "this.src= ' {{URL (' captcha/mews ')}}?r= ' +math.random ();"alt=""><!--      -<inputtype= "Submit"value= "Submit" />    </form>    <DivID= "Footer"style= "Text-align:center; border-top:dashed 3px #eeeeee; margin:50px 0; padding:20px;">©2015<ahref= "http://www.cnblogs.com/Zell-Dinch/">Zelldincht</a>    </Div>    <!--Scripts -    <Scriptsrc= "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></Script>    <Scriptsrc= "//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></Script></Body></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; Useilluminate\http\request; Useapp\http\requests; UseApp\http\controllers\controller; UseInput, Validator, Redirect, Session,Captcha;classTestControllerextendscontroller{ Public functionCPT (Request$request) {        //DD (input::get (' CPT '));        $rules= [            "CPT" = ' Required|captcha '        ]; $messages= [            ' cpt.required ' = ' Please enter the verification code ', ' Cpt.captcha ' and ' = ' Verification code error, please retry '        ]; If you only verify the value of CAPTCHA, you can
Using Captcha::check (Input::get (' CPT ')); returns BOOL
$validator= Validator::make (Input::all (),$rules,$messages); if($validator-fails ()) { returnRedirect::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.

If you have help, please click on the recommended ^_^, thank you!

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.