long399 / yii2-math-captcha
yii2 MathCaptchaAction
0.1.2
2022-05-05 08:38 UTC
Requires
- yiisoft/yii2: *
README
为 Yii 框架 2.0 设计的 MathCaptchaAction
安装
安装此扩展的最佳方式是通过 composer。运行以下命令之一:
composer require --prefer-dist long399/yii2-math-captcha
或者
"long399/yii2-math-captcha": "~0.1"
将以下内容添加到您的 composer.json
文件的 require 部分:
可用的操作
- 加法。
- 减法。
- 乘法。
默认将随机使用加法或减法操作。
使用方法
控制器
public function actions() { return [ ... 'captcha' => [ 'class' => \long399\captcha\MathCaptchaAction::class, 'fixedVerifyCode' => YII_ENV_TEST ? '399' : null, 'minLength' => 0, 'maxLength' => 1000, ], ... ]; }
模型
class MyModel extends \yii\db\ActiveRecord { public $captcha; ... public function rules() { return [ ... ['captcha', 'captcha', 'captchaAction' => '/site/captcha'], ... ]; } ... }
视图
... echo $form->field($model, 'captcha')->widget(\yii\captcha\Captcha::class, [ 'captchaAction' => "/site/captcha", 'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>', ]); ...
如果您只想使用减法运算的表达式,例如,则需要在控制器中操作描述中相应地设置 operations 属性
public function actions() { return [ ... 'captcha' => [ 'class' => \app\components\actions\MathCaptchaAction::class, 'fixedVerifyCode' => YII_ENV_TEST ? '399' : null, 'minLength' => 0, 'maxLength' => 1000, 'operations' => ['-'], ], ... ]; }
带乘法的表达式
如果您还想使用乘法运算的表达式,则需要在控制器中操作描述中相应地设置 operations 属性
public function actions() { return [ ... 'captcha' => [ 'class' => \app\components\actions\MathCaptchaAction::class, 'fixedVerifyCode' => YII_ENV_TEST ? '399' : null, 'minLength' => 0, 'maxLength' => 1000, 'operations' => ['+', '-', '*'], ], ... ]; }
可以生成三种类型的表达式
作者
long399,电子邮件:long399@mail.ru