在使用TP5官方的驗證碼類庫時經常會遇到驗證碼無法顯示的問題,最常見的就是路徑問題和版本問題
路徑問題:
當使用composer require topthink/think-captcha命令安裝完驗證碼類庫后,注意打開hepler.php文件查看一下路徑,前三條的路徑有時候沒有指定位置:
Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
Validate::extend('captcha', function ($value, $id = "") {
return captcha_check($value, $id, (array)\think\Config::get('captcha'));
});
Validate::setTypeMsg('captcha', '驗證碼錯誤!');
還有方法里:
function captcha_src($id = "")
{
return Url::build('/captcha' . ($id ? "/{$id}" : ''));
}
這就需要我們手動在類前面添加\think\的路徑,變為:
\think\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
\think\Validate::extend('captcha', function ($value, $id = "") {
return captcha_check($value, $id, (array)\think\Config::get('captcha'));
});
\think\Validate::setTypeMsg('captcha', '驗證碼錯誤!');
function captcha_src($id = "")
{
return \think\Url::build('/captcha' . ($id ? "/{$id}" : ''));
}
版本問題:
現在使用composer require topthink/think-captcha命令后下載安裝完成的驗證碼插件是2.0移上的版本,只適用於TP5.1,TP5.0是不行的,這就需要我們卸載掉下載的2.0版本后,再下載安裝1.x版本的驗證碼類庫,具體命令如下:
卸載2.0:composer remove topthink/think-captcha
下載1.x:composer require topthink/think-captcha 1.*
這樣安裝完成后就可以正常顯示驗證碼啦