swjtuhyq / captcha
Lumen 9 & 10 验证码包
2.0.0
2024-03-19 04:01 UTC
Requires
- php: ^8.1
- ext-gd: *
- illuminate/config: ^9|^10|^11
- illuminate/filesystem: ^9|^10|^11
- illuminate/hashing: ^9|^10|^11
- illuminate/session: ^9|^10|^11
- illuminate/support: ^9|^10|^11
- intervention/image: ~3.5
Requires (Dev)
- mockery/mockery: ^1.4.4
- phpunit/phpunit: ^10.0
README
一个简单的 Lumen 9/10 服务提供者,用于包含 Captcha for Lumen。
安装
可以通过使用 Composer 并在您的项目中的 composer.json
中设置 minimum-stability
为 dev
(Laravel 5 需要)来安装 Captcha 服务提供者,要求 swjtuhyq/captcha
包。
{ "require": { "laravel/lumen-framework": "^10.0", "swjtuhyq/captcha": "~2.0" }, "minimum-stability": "dev" }
或者
使用 composer 需求此包
composer require swjtuhyq/captcha
使用 composer update
更新包或使用 composer install
安装。
在 Windows 中,您需要在 php.ini 中包含 GD2 DLL php_gd2.dll
。您还需要包含 php_fileinfo.dll
和 php_mbstring.dll
以满足 swjtuhyq/captcha
依赖项的要求。
用法
要使用 Captcha 服务提供者,您必须在启动 Laravel 应用程序时注册提供者。有本质上两种方式来做这件事。
在 config/app.php
中找到 providers
键并注册 Captcha 服务提供者。
'providers' => [ // ... 'Swjtuhyq\Captcha\CaptchaServiceProvider', ]
在 config/app.php
中找到 aliases
键。
'aliases' => [ // ... 'Captcha' => 'Swjtuhyq\Captcha\Facades\Captcha', ]
配置
要使用自己的设置,请发布配置。
config/captcha.php
return [ 'default' => [ 'length' => 5, 'width' => 120, 'height' => 36, 'quality' => 90, 'math' => true, //Enable Math Captcha 'expire' => 60, //Stateless/API captcha expiration ], // ... ];
示例用法
无状态模式
您可以从这个 URL 获取键和图像 https:///captcha/api/math
并使用此方法验证验证码
//key is the one that you got from json response // fix validator // $rules = ['captcha' => 'required|captcha_api:'. request('key')]; $rules = ['captcha' => 'required|captcha_api:'. request('key') . ',math']; $validator = validator()->make(request()->all(), $rules); if ($validator->fails()) { return response()->json([ 'message' => 'invalid captcha', ]); } else { //do the job }
返回图像
captcha();
或者
Captcha::create();
返回 URL
captcha_src();
或者
Captcha::src('default');
返回 HTML
captcha_img();
或者
Captcha::img();
要使用不同的配置
captcha_img('flat'); Captcha::img('inverse');
等。
基于 Captcha for Laravel 5/6/7/8
^_^