long399/yii2-math-captcha

yii2 MathCaptchaAction

0.1.2 2022-05-05 08:38 UTC

This package is auto-updated.

Last update: 2024-09-05 13:51:07 UTC


README

Total Downloads License

为 Yii 框架 2.0 设计的 MathCaptchaAction

Screenshot

安装

安装此扩展的最佳方式是通过 composer。运行以下命令之一:

 composer require --prefer-dist long399/yii2-math-captcha

或者

"long399/yii2-math-captcha": "~0.1"

将以下内容添加到您的 composer.json 文件的 require 部分:

可用的操作

  1. 加法。
  2. 减法。
  3. 乘法。

默认将随机使用加法或减法操作。

使用方法

控制器

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' => ['+', '-', '*'],
        ],
        ...
    ];
}

可以生成三种类型的表达式

  1. 带乘法的表达式。
    Screenshot
  2. 带乘法和加法的表达式。
    Screenshot
  3. 带乘法和减法的表达式。
    Screenshot

作者

long399,电子邮件:long399@mail.ru