Use the Captcha extension package to generate a verification code for the Laravel5 application

Source: Internet
Author: User
Use the Captcha extension package to generate a verification code for the Laravel5 application. 1. install

We use Composer to install the Captcha extension package:

composer require mews/captcha

Note: To use this extension package in Windows, you also need to install the GD2 extension (remove the comment before php_gd2.dll in php. ini ).

2. configuration

Before using the Captcha service provider, you must register the service provider in config/app. php:

'providers' => [    // ...    Mews\Captcha\CaptchaServiceProvider::class,]

Register the corresponding facade at the same time:

'aliases' => [    // ...    'Captcha' => Mews\Captcha\Facades\Captcha::class,]

If you want to use custom configuration, you can also publish the configuration file to the config Directory:

$ php artisan vendor:publish

Edit the newly generated captcha. php:

return [    'default' => [        'length' => 5,        'width' => 120,        'height' => 36,        'quality' => 90,    ],    // ...];
3. example
// app/Http/routes.phpRoute::any('captcha-test', function(){    if (Request::getMethod() == 'POST')    {        $rules = ['captcha' => 'required|captcha'];        $validator = Validator::make(Input::all(), $rules);        if ($validator->fails())        {            echo '

Incorrect!

'; } else { echo '

Matched :)

'; } } $form = ''; return $form;});

The display effect is as follows:

To return a native image, you can call this function:

captcha();

Or

Captcha::create();

If you want to return a URL:

captcha_src();

Or

Captcha::src();

If you want to return HTML:

captcha_img();

In this example, we use this function or call the method on the Captcha facade:

Captcha::img();

To use different configuration items in the configuration file captcha. php, you can call:

captcha_img('flat');Captcha::img('inverse');

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.