1. 在laravel项目根目录下找到 composer.json 这个文件
2. "gregwar/captcha": "1.*" 到composer.json这个文件中,如下图所示
然后打开命令行,找到项目的根目录,运行
composer update
3. 接下来,就可以正常使用验证码了,先定义路由:
Route::get('/captcha', 'CodeController@captcha');
4. 在控制层里新建一个codeController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder; //引用对应的命名空间
use Session;
class CodeController extends Controller{
public function captcha()
{
$builder = new CaptchaBuilder();
$builder->build(150,32);
$phrase = $builder->getPhrase(); //获取验证码内容
Session::put('milkcaptcha', $phrase); //把内容存入session
ob_clean(); //清除缓存
//把验证码数据以jpeg图片的格式输出
return response($builder->output())->header('content-type','image/jpeg');
}
}
5. 在表单里显示验证码,把上面该文件路径写到<img>标签的src属性中
<img src="{!! URL('/captcha') !!}" alt="验证码"onclick="this.src='{{ URL('/captcha') }}?t='+ Math.random()">
网友评论