thinkphp6验证码
2022-11-24 11:10:29
137
{{single.collect_count}}

验证码安装命令行

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');}

 

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}