klintlili / yii2-captcha
为 Yii2 的验证码库包装器
dev-master
2020-11-19 01:44 UTC
Requires
- gregwar/captcha: 1.*
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-19 10:41:46 UTC
README
Gregwar 的验证码库包装器 Gregwar's Captcha library,用于 Yii2。
安装
安装此扩展的首选方式是通过 composer。运行以下命令之一:
php composer.phar require --prefer-dist klintlili/yii2-captcha "*"
或者将以下内容添加到您的 composer.json
文件的 require 部分:
"klintlili/yii2-captcha": "*"
使用方法
此扩展有 3 个不同的步骤。首先是调用 CaptchaAction
生成验证码图片,然后在视图中渲染验证码图片,最后将用户输入与生成的验证码代码进行验证。以下是设置此扩展的每个步骤的说明:
操作
将以下方法添加到您的 Controller 中。
public function actions() { return [ 'captcha' => [ 'class' => 'klintlili\captcha\CaptchaAction', //'length' => 5, // captcha character count //'width' => 150, // width of generated captcha image //'height' => 40, // height of generated captcha image ], ]; }
一些可配置的属性包括:
length
设置生成验证码字符计数的整数值width
设置生成验证码图片的宽度height
设置生成验证码图片的高度
视图文件
将以下代码添加到您的视图中以渲染验证码图片和输入。
use klintlili\captcha\Captcha; ... <?php echo Captcha::widget([ 'model' => $model, 'attribute' => 'captcha', //'captchaAction' => 'site/captcha', // captcha action, default to site/captcha //'template' => '{image} {input}', // template for rendering CAPTCHA image and input //'options' => [ // HTML attribute for rendering text input // 'class' => 'form-control', //], ]) ?>
您还可以使用 ActiveForm 实例来渲染验证码输入。
use klintlili\captcha\Captcha; ... <?php echo $form->field($model, 'captcha')->widget(Captcha::className()) ?>
一些可配置的属性包括:
captchaAction
验证码操作,默认为site/captcha
template
渲染验证码图片和输入的模板。在此模板中,占位符{image}
将替换为实际图片标签,而{input}
将替换为文本输入标签。options
输入标签的 HTML 属性。
验证
将以下规则添加到您的模型中以验证验证码输入
use klintlili\captcha\CaptchaValidator; ... public function rules() { return [ ... some other rules... ['captcha', CaptchaValidator::className()], ]; }