验证码安装命令行
composer require topthink/think-captcha
如果是做api接口,验证码安装好后要配置路由,配置规则如下:
//验证码路由Route::get('captcha','Login/captcha');
多应用路由
//多应用路由Route::get('captcha/[:config]','\\think\\captcha\\CaptchaController@index');
验证码需要开启全局session
打开app/middleware.php文件,将\think\middleware\SessionInit::class 注释去掉就可以了
<?php// 全局中间件定义文件return [// 全局请求缓存// \think\middleware\CheckRequestCache::class,// 多语言加载// \think\middleware\LoadLangPack::class,// Session初始化 \think\middleware\SessionInit::class];
自定义验证码需引用门面模式下的Captcha中的create()创建验证码
use think\captcha\facade\Captcha;class Login extends Common{/** * 自定义验证码 * * @return void */public function captcha(){//生成验证码$captcha = Captcha::create();//生成验证码路径$url = Request::url(true);//生成数据集$data = [$url,$captcha];//返回api接口return $this->ReturnApi($data,'验证码请求成功!',200,'json');}