juliardi / yii2-captcha
Yii2 的验证码库包装器
1.1.1
2020-08-18 14:20 UTC
Requires
- gregwar/captcha: 1.*
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-09 02:46:39 UTC
README
Yii2 Captcha 使用 Gregwar 的 Captcha 库 包装器为 Yii2。
目录
安装
该包可在 Packagist 上获取,您可以使用 Composer 进行安装。
composer require juliardi/yii2-captcha "*"
或者在您的 composer.json
文件的 require 部分中添加。
"juliardi/yii2-captcha": "*"
用法
此扩展有3个不同的步骤。首先是通过调用 juliardi\captcha\CaptchaAction
来提供 CAPTCHA 图像 - 防止网站垃圾邮件的一种方式,然后通过 juliardi\captcha\Captcha
在视图中渲染 CAPTCHA 图像,最后使用 juliardi\captcha\CaptchaValidator
验证用户输入与生成的 CAPTCHA 代码是否匹配。
以下是设置此扩展的每个步骤的说明
操作
将以下方法添加到您的控制器中。
public function actions() { return [ 'captcha' => [ 'class' => \juliardi\captcha\CaptchaAction::class, /** * How many times should the same CAPTCHA be displayed. Defaults to 3. * A value less than or equal to 0 means the test is unlimited (available since version 1.1.2). */ 'testLimit' => 3, // int /** * The width of the generated CAPTCHA image. Defaults to 150. */ 'width' => 150, // int /** * The height of the generated CAPTCHA image. Defaults to 40. */ 'height' => 40, // int /** * The minimum & maximum length for randomly generated word. Defaults to [5, 7] | min 5 max 7. * * If an array is provided, the first value will be used as the minimum length and the second value will be used as the maximum length. * * **Note:** The minimum length must be at least 3 and the maximum length must be at most 20. * */ 'length' => [5, 7], // int|int[] | // Random word length will be between 5 and 7 characters /** * The quality of the generated JPEG image. Valid values are 1 - 100. Defaults to 80. */ 'quality' => 80, // int /** * The fixed verification code. When this property is set, * * This is mainly used in automated tests where we want to be able to reproduce * the same verification code each time we run the tests. * If not set, it means the verification code will be randomly generated. */ // 'fixedVerifyCode' => 'testme', // string|null ], ]; }
视图
将以下代码添加到您的视图中以渲染 CAPTCHA 图像和输入。
以下示例演示了如何使用此小部件与模型属性一起使用
use juliardi\captcha\Captcha; echo Captcha::widget([ 'model' => $model, 'attribute' => 'captcha', // configure additional widget properties here /** * The route of the action that generates the CAPTCHA images. * The action represented by this route must be an action of [[CaptchaAction]]. * Please refer to [[\yii\helpers\Url::toRoute()]] for acceptable formats. */ 'captchaAction' => 'site/captcha', // string|array /** * HTML attributes to be applied to the CAPTCHA image tag. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. */ 'imageOptions' => [], // array /** * The template for arranging the CAPTCHA image tag and the text input tag. * In this template, the token `{image}` will be replaced with the actual image tag, * while `{input}` will be replaced with the text input tag. */ 'template' => '{image} {input}', // string /** * HTML attributes for the input tag. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. */ 'options' => ['class' => 'form-control'], // array ]);
以下示例将使用名称属性代替
use juliardi\captcha\Captcha; echo Captcha::widget([ 'name' => 'captcha', ]);
您还可以使用此小部件在 ActiveForm 中,使用 widget() 方法,例如
<?= $form->field($model, 'captcha')->widget(\juliardi\captcha\Captcha::class, [ // configure additional widget properties here ]) ?>
验证
将以下规则添加到您的模型中以验证 CAPTCHA 输入
use juliardi\captcha\CaptchaValidator; public function rules() { return [ ... some other rules... ['captcha', CaptchaValidator::class], ]; }