The following editor will bring you an example of how to generate verification codes in laravel5.4. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look
Summary: This article introduces the specific steps of using gregwar/captcha to implement verification codes, as well as possible problems and solutions.
Operation steps:
1, find composer.json in the laravel5.4 project root directory This file,
add
"gregwar/captcha": "1.*" to composer.json this file, as shown below.
2. Then open the command line, find the root directory of the project, run composer update,
##You can see that this 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: 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; //引用对应的命名空间 use Gregwar\Captcha\CaptchaBuilder; use Session; class CodeController extends Controller{ public function captcha($temp) { $builder = new CaptchaBuilder(); $builder->build(150,32); $phrase = $builder->getPhrase(); //把内容存入session Session::flash('milkcaptcha', $phrase); //存储验证码 ob_clean(); return response($builder->output())->header('Content-type','image/jpeg'); } }
Still execute composer update in the project root directory, and then execute the composer dump-autoload command. The same effect can be achieved. Finally, let me talk about the problems I have encountered. Many pictures of generating laravel verification codes on the Internet are written like this.
public function code($tmp) { //生成验证码图片的Builder对象,配置相应属性 $builder = new CaptchaBuilder; //可以设置图片宽高及字体 $builder->build($width = 100, $height = 40, $font = null); //获取验证码的内容 $phrase = $builder->getPhrase(); //把内容存入session Session::flash('milkcaptcha', $phrase); //生成图片 header("Cache-Control: no-cache, must-revalidate"); header('Content-Type: image/jpeg'); $builder->output(); }
public function captcha($temp) { $builder = new CaptchaBuilder(); $builder->build(150,32); $phrase = $builder->getPhrase(); //把内容存入session Session::flash('milkcaptcha', $phrase); //存储验证码 ob_clean(); return response($builder->output())->header('Content-type','image/jpeg'); }
The above is the detailed content of Detailed explanation of verification code generation using laravel5.4. For more information, please follow other related articles on the PHP Chinese website!