tp6中使用think-captcha在控制器中获取不到验证码

  • 环境配置 : tp6 lamp

  • 问题描述 : 在控制器中获取不到生成的验证码

  • 尝试过的方法 :

    1. 按照官方文档中的方法
      tp6中使用think-captcha在控制器中获取不到验证码

      • 报错 : 找不到该函数,查找源码后,发现该函数定义在验证码源码中的helper文件中,而该文件并没有命名空间,而即便我用 require_once 引入也不行;我也尝试过把 captcha函数中的代码复制到控制器中,也不行
    2. 尝试调用生成验证码类 Captcha

      use think\captcha\Captcha;
      public function getCaptcha()
      {
          return (new Captcha())->create();
      }
      • 报错如下 :
        tp6中使用think-captcha在控制器中获取不到验证码
      • Captcha文件的命名空间如下
        tp6中使用think-captcha在控制器中获取不到验证码

  • 所以求各位大哥指教下这个到底是怎么回事?
讨论数量: 6
use think\captcha\Captcha;
public function getCaptcha(Captcha $captcha)
{
    return $captcha->create();
}
3年前 评论
qingshui (楼主) 3年前

在你的测试页面使用以下形式试试

<div>{:captcha_img()}</div>
3年前 评论
qingshui (楼主) 3年前
cn-five (作者) 3年前
qingshui (楼主) 3年前

helper.php 不需要命名空间,这个文件导入了足够多的类。

3年前 评论
qingshui (楼主) 3年前
zxp_lethe

composer dump-autoload 试一下

3年前 评论
qingshui (楼主) 3年前

think-captcha 模块里不是有一个 CaptchaController.php 文件吗,可以去研究下,能得出你想要的结果。

3年前 评论

你引用的类有问题,所以会报找不到 think\captcha\Captcha

namespace app\testcaptcha\controller;

use app\BaseController;
use think\captcha\facade\Captcha;

class Verify extends BaseController
{
    public function  getCaptcha()
    {
        return Captcha::create();
    }
}

在你的 lamp 环境下访问 你的域名或者 ip /testcaptcha/verify/getcaptcha

3年前 评论
qingshui (楼主) 3年前
cn-five (作者) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!