Home  >  Article  >  PHP Framework  >  How to install verification code package in laravel

How to install verification code package in laravel

藏色散人
藏色散人Original
2021-12-22 11:45:562332browse

How to install the verification code package in laravel: 1. Log in to the website packagist.org; 2. Search for laravel captcha and find "mews/captcha"; 3. Install the verification code according to the usage method on packagist. .

How to install verification code package in laravel

The operating environment of this article: Windows 7 system, Laravel version 5.7, DELL G3 computer.

How to install the verification code package in laravel?

Laravel - Captcha

  • # I feel that I use better verification code bags, take it out to share, fool -style tutorial, big guy don't spray. :smile: :smile: :smile:
  • Installation steps:
    • First, log in to the website packagist.org and find laravel captcha, find mews/captcha , and install the verification code step by step according to the usage method on packagist.
    • composer installation: composer require mews/captcha
    • Registrationproviders (config/app .php) , append the following code at the end of this array:
      Mews\Captcha\CaptchaServiceProvider::class,
    • Register aliases (config/app.php), and append the following code to the end of this array:
      'Captcha' => Mews\Captcha \Facades\Captcha::class,
    • Generate the configuration file and enter the following command in the Composer command line:
      php artisan vendor:publish
    • Enter the config/captcha.php file and modify the default array to style the verification code , modifications in quantity and size.
      'default'   => [
      'length'    => 5,
      'width'     => 100,
      'height'    => 34,
      'quality'   => 90,
      ],
  • Used in the page:
<div class="row">
    <div class="col-md-8">
        <input type="text" class="form-control {{$errors->has(&#39;captcha&#39;)?&#39;parsley-error&#39;:&#39;&#39;}}" name="captcha" placeholder="captcha">
    </div>
    <div class="col-md-4">
        <img src="{{captcha_src()}}" style="cursor: pointer" onclick="this.src=&#39;{{captcha_src()}}&#39;+Math.random()">
    </div>
    @if($errors->has(&#39;captcha&#39;))
        <div class="col-md-12">
            <p class="text-danger text-left"><strong>{{$errors->first(&#39;captcha&#39;)}}</strong></p>
        </div>
    @endif
</div>
  • Click the image to refresh, the following code:
5ab40362dfc18e2787c7c43b0597315f
  • Rewrite the AuthController login verification method and customize the prompt message:

    • First introduce the following code:
      use Illuminate\Http\Request;

    • Rewrite the validateLogin method:

 protected function validateLogin(Request $request){
        $this->validate($request, [
            $this->loginUsername() => 'required',
            'password' => 'required',
            'captcha' => 'required|captcha',
        ],[
            'captcha.required' => trans('validation.required'),
            'captcha.captcha' => trans('validation.captcha'),
        ]);
    }
  • Downloading and switching of font libraries:
    • First you need to download the font library
    • After the download is completed, copy the src/zh-CN folder in the compressed package to the resources/lang folder in the project directory.
    • Modify the config->app.php file and modify the code as follows:
      'locale' => 'zh-CN',
  • Since captcha does not have a Chinese explanation in the Chinese package, you need to manually add a Chinese explanation. The specific operations are as follows:
    • Open resources/zh-CN/validation.php, append the following key-value pairs to the total array:
      'captcha'                  => ':attribute 不正确。',
    • In attributes Append the following key-value pairs to the array:
      'captcha'               => '验证码',
Related recommendations: The latest five Laravel video tutorials                                      

The above is the detailed content of How to install verification code package in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn