Laravel5.4 generates the verification code and laravel5.4 the verification code.
Summary: This blog introduces how to use gregwar/captcha to implement the verification code, as well as possible problems and solutions.
Procedure:
1. Find the file composer. json in the root directory of the laravel5.4 project,
Add
"Gregwar/captcha": "1. *" to the composer. json file, as shown in.
2. Open the command line, find the project root directory, and run composer update,
You can see that the extension library has been downloaded,
3. Next, you can use the verification code normally. First, test whether the verification code can be displayed normally,
First define the route:
Create a new codeController. php In the control layer,
1 <? Php 2 namespace App \ Http \ Controllers; 3 4 use App \ Http \ Requests; 5 use App \ Http \ Controllers \ Controller; 6 7 use Illuminate \ Http \ Request; 8 9 // reference the corresponding namespace 10 use Gregwar \ Captcha \ CaptchaBuilder; 11 use Session; 12 class CodeController extends Controller {13 public function captcha ($ temp) 14 {15 $ builder = new CaptchaBuilder (); 16 $ builder-> build (150,32); 17 $ phrase = $ builder-> getPhrase (); 18 // Save the content to session19 Session: flash ('milkcaptcha ', $ phrase); // store the Verification Code 20 ob_clean (); 21 return response ($ builder-> output ()-> header ('content-type', 'image/jpeg '); 22} 23 24}
Then access the previously defined route in the browser and you will be able to see the verification code.
In addition, it can be written in composer. json as follows,
Or execute composer update in the project root directory, and then execute the composer dump-autoload command.
The same effect can be achieved.
Finally, let's talk about the problems I encountered. Many images that generate laravel verification codes on the Internet are written in this way,
Public function code ($ tmp) {// Builder object for generating the verification code image, and configure the corresponding attribute $ builder = new CaptchaBuilder; // you can set the image width and height and font $ builder-> build ($ width = 100, $ height = 40, $ font = null ); // obtain the content of the Verification Code $ phrase = $ builder-> getPhrase (); // Save the content to sessionSession: flash ('milkcaptcha ', $ phrase ); // generate the image header ("Cache-Control: no-cache, must-revalidate"); header ('content-Type: image/jpeg '); $ builder-> output ();}
I tried it, and the result verification code is garbled and the picture is not displayed, for example:
Later, I changed it.
Public function captcha ($ temp) {$ builder = new CaptchaBuilder (); $ builder-> build (150,32); $ phrase = $ builder-> getPhrase (); // Save the content to the session Session: flash ('milkcaptcha ', $ phrase); // store the verification code ob_clean (); return response ($ builder-> output ()) -> header ('content-type', 'image/jpeg ');}
It will be displayed normally.