Use captcha to introduce verification code in laravel framework

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

Because of the English slag level five, I almost didn't find anything useful, so I considered searching for the verification code package on github!

Tip: There are often instructions for use in the package on github, and you can generally make it out according to the above implementation.

 

I am using mews captcha

The address on git: https://github.com/mewebstudio/captcha The  above usage is very detailed.

 

Hands-on implementation:

- Manually enter the laravel project directory

 

- In the corresponding directory, find the composer.json file, open it, and add the following statement:

As shown in the figure: add statement

{
    "require": {
        ....
        ....
        ....
        "mews/captcha": "~2.0"
    },
    "minimum-stability": "dev"
}    

- Execute composer update, if an error is reported, please execute composer self-update first, and then execute composer update, if it still fails, just composer install

 

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

1. Open this file, find the provider item, and add the following statement

Mews\Captcha\CaptchaServiceProvider::class,

2. Find the aliases item and add the following statement:

Add statement:

'Captcha' => Mews\Captcha\Facades\Captcha::class,

- Execute php artisan vendor:publish in the cmd window

 

Through all the above steps, check the directory, you will find that there is more mews directory under the vendor, and you can use the namespace use Captcha, there is a configuration file captcha.php with verification code in config/captcha.php.

 

 

- All the above steps  also exist in  https://github.com/mewebstudio/captcha !

 

Let's take a look at the newly added vendor/mews directory

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

Find the public method and find:

public function create($var) --- Generate verification code

public function check($var) --- Determine whether the input and the verification code are equal

public function src($var) --- output link of img attribute src

public function img($var) --- output img tag

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

Default, flat, mini, inverse represent four styles of verification codes! Can be configured for different styles, directly modify the configuration items in config/captcha.php

Call of the view layer:

@if($errors->has('captcha'))
            <div class="col-md-12">
              <p class="text-danger text-left"><strong>{
   
   {$errors->first('captcha')}}</strong></p>
            </div>
          @endif
          <img src="{
   
   {captcha_src()}}" onclick="this.src='{
   
   {captcha_src()}}'+Math.random()" title="点击图片重新获取验证码">

validate verification:

public function validatecap($data){
        $validator = Validator::make($data, [
            // 如何在这里转换数据
            'captcha' => 'required|captcha',
        ],[
            'captcha.required' => '请填写验证码',
            'captcha.captcha' => '验证码错误',
        ]);

        if($validator->fails()){
            return $validator->messages();
        }
    }

 

Guess you like

Origin blog.csdn.net/qq_43737121/article/details/109058182