riiitor / yii2-antispam
由CleanTalk提供的yii2反垃圾扩展,具有防止垃圾邮件机器人及手动垃圾邮件的功能
v1.1.0
2019-07-08 13:52 UTC
Requires
Requires (Dev)
- phpunit/phpunit: 6
- yiisoft/yii2: ~2.0.0
This package is not auto-updated.
Last update: 2024-09-25 14:35:52 UTC
README
无需验证码,无需回答问题,无需计数动物,无需解谜,无需数学计算。
##需求
Yii 2.0 或更高版本
##安装
安装此扩展的最佳方式是通过 composer。
运行以下命令
php composer.phar require --prefer-dist cleantalk/yii2-antispam
或在您的composer.json文件的require部分添加以下内容
"cleantalk/yii2-antispam": "~1.0.0"
##使用方法
-
在protected/config/web.php中打开您的应用程序配置,并修改components部分
// application components
'components'=>[
...
'antispam' => [
'class' => 'cleantalk\antispam\Component',
'apiKey' => 'Your API KEY',
],
...
],
- 在您的模型中添加验证器,例如 ContactForm
namespace app\models;
use cleantalk\antispam\validators\MessageValidator;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $body;
/**
* @inheritdoc
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
['body', MessageValidator::className(), 'emailAttribute'=>'email', /*'nickNameAttribute'=>'name'*/]
];
}
}
- 在表单视图中添加隐藏的JavaScript检查小部件
<?php $form = ActiveForm::begin(); ?>
...
<?= \cleantalk\antispam\Widget::widget()?>
...
<?= Html::submitButton('Submit')?>
...
<?php ActiveForm::end(); ?>
用户注册验证器
查看 cleantalk\antispam\validators\UserValidator
示例规则
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'email'], 'required'],
['email', 'email'],
['email', UserValidator::className(), 'nickNameAttribute'=>'name']
];
}
##许可证 GNU通用公共许可证
##资源