This article is mainly for everyone to bring an example of laravel5.4 generation verification code to explain. Small series feel very good, now share to everyone, also for everyone to make a reference. Follow the small series together to see it, hope to help everyone.
Summary: This article describes the steps to implement a verification code using Gregwar/captcha, as well as the problems and workarounds you might encounter.
Operation Steps:
1, locate the Composer.json file under the laravel5.4 project root directory,
Add to
"Gregwar/captcha": "1.*" to composer.json this file, as shown in.
2. Then open the command line, locate the project's root directory, run the composer update,
You can see that the extension library has been downloaded.
3. Next, you can use the verification code, first test the verification code can be displayed correctly,
Define the route first:
And then create a new codecontroller.php in the control layer,
<?php namespace App\http\controllers;use app\http\requests;use app\http\controllers\controller;use Illuminate\ http\request;//referencing the corresponding namespace use Gregwar\captcha\captchabuilder;use Session;class Codecontroller extends Controller{ Public Function Captcha ($temp) { $builder = new Captchabuilder (); $builder->build (150,32); $phrase = $builder->getphrase (); The contents are deposited in Session session::flash (' Milkcaptcha ', $phrase);//Storage Verification Code Ob_clean (); Return Response ($builder->output ())->header (' Content-type ', ' image/jpeg '); }}
Then you can see the verification code by accessing the previously defined route in the browser.
In addition, it can be written in Composer.json,
The composer update is executed at the project root, and then the composer Dump-autoload command is executed.
The same effect can be achieved.
Finally, to say that I have encountered problems, many online generated laravel verification code pictures are written in this way,
Public Function Code ($TMP) {//Generate the Builder object for the CAPTCHA picture, configure the corresponding property $builder = new captchabuilder;//to set the picture width and font $builder->build ( $width =, $height = all, $font = null);//Get the contents of the verification code $phrase = $builder->getphrase ();//Deposit the Contents Sessionsession::flash (' Milkcaptcha ', $phrase);//Generate Picture Header ("Cache-control:no-cache, must-revalidate"); header (' Content-type:image/jpeg ') ; $builder->output ();}
I followed the test, the results of verification code image display garbled, do not display pictures, such as:
I changed it later.
Public Function Captcha ($temp) { $builder = new Captchabuilder (); $builder->build (150,32); $phrase = $builder->getphrase (); The contents are deposited in Session session::flash (' Milkcaptcha ', $phrase);//Storage Verification Code Ob_clean (); Return Response ($builder->output ())->header (' Content-type ', ' image/jpeg '); }
It can be displayed normally.