cleantalk / yii2-antispam
CleanTalk 提供的防止垃圾邮件的 yii2 扩展,可防止垃圾邮件爬虫和人工垃圾邮件
v1.0.0
2015-06-30 17:54 UTC
Requires
- cleantalk/php-antispam: ~2.0.0
Requires (Dev)
- phpunit/phpunit: ~4.6.9
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-12 08:39:24 UTC
README
无需验证码,无需回答问题,无需数动物,无需解谜,无需数学计算。
要求
- Yii 2.0 或更高版本
- CleanTalk 账户 https://cleantalk.org/register?product=anti-spam
##安装
通过 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 通用公共许可证
##资源