makeitfly / cleantalk-symfony
Cleantalk 防垃圾邮件集成用于 Symfony
0.1.0
2022-01-06 15:39 UTC
Requires
- php: >=7.4
- cleantalk/php-antispam: ^2.6
Suggests
- symfony/property-access: Required when not using callables to configure the CleanTalkType
README
非官方的 Cleantalk 防垃圾邮件集成用于 Symfony。目前仅提供最基础的防垃圾邮件保护,如 php-antispam 包的文档所述。
安装
确保全局已安装 Composer,如 Composer 文档中的 安装章节 所述。
默认情况下,您的 Symfony 应用程序在执行 composer require
后自动执行 cache:clear
。因为我们还没有提交 Flex 食谱(yet),所以这会因为缺少配置而出错。目前,理想的做法是在您要求捆绑包之前创建配置文件,如 配置部分 所述。
使用 Symfony Flex 的应用程序
打开命令行,进入您的项目目录并执行
$ composer require makeitfly/cleantalk-symfony
未使用 Symfony Flex 的应用程序
步骤 1:下载捆绑包
打开命令行,进入您的项目目录并执行以下命令以下载此捆绑包的最新稳定版本
$ composer require makeitfly/cleantalk-symfony
步骤 2:启用捆绑包
然后,通过将其添加到项目 config/bundles.php
文件中注册的捆绑包列表中来启用捆绑包
// config/bundles.php return [ // ... MakeItFly\CleanTalkBundle\MakeItFlyCleanTalkBundle::class => ['all' => true], ];
配置
您只需要一个认证密钥,您可以在 CleanTalk 控制台中找到。
# config/packages/makeitfly_cleantalk.yaml makeitfly_cleantalk: enabled: true auth_key: '%env(MAKEITFLY_CLEANTALK_AUTH_KEY)%' # Other optional config. server_url: "https://moderate.cleantalk.org/api2.0/" # CleanTalk API endpoint agent: "makeitfly-symfony" # Sent on every API request
建议创建一个开发配置,该配置禁用您的开发环境中的验证。
# config/packages/dev/makeitfly_cleantalk.yaml makeitfly_cleantalk: enabled: false
使用方法
将 CleanTalkType
添加为表单的字段。当您调用 $form->isValid()
时,它将自动定义一个约束,该约束将被验证。
示例用法
public function buildForm( FormBuilderInterface $builder, array $options ): void { $builder ->add('message', TextareaType::class, [ 'label' => 'form.contact.message.label' ]) ->add('email', EmailType::class, [ 'label' => 'form.contact.email.label' ]) ->add('cleantalk', CleanTalkType::class, [ 'sender_email_field' => 'email', 'message_field' => 'message' ]); }
传递表单数据
CleanTalk API 会检查提交的表单值是否为垃圾邮件。发送者电子邮件是唯一必需的字段,但建议传递尽可能多的数据以提高垃圾邮件检测能力。
当您将这些字段配置为字符串时,库将使用这些字段从表单数据中获取数据。这需要安装 symfony/property-access
。
或者,您可以使用回调函数自行返回自定义值。
// Minimal configuration: $builder->add('cleantalk', CleanTalkType::class, [ 'sender_email_field' => 'email', // Required ]); // Full configuration: $builder->add('cleantalk', CleanTalkType::class, [ // One of CleanTalkCheck::MESSAGE or CleanTalkCheck::USER. This defines // which CleanTalk API endpoint is used: // @see https://cleantalk.org/help/api-check-message // @see https://cleantalk.org/help/api-check-newuser 'check_type' => CleanTalkCheck::MESSAGE, // The email address of the person who submits the form. This will be // fetched from the 'someProperty' property of the form data. 'sender_email_field' => 'someProperty', // Alternatively, for this and the following properties you can use a // callable instead, which will get passed the form data. 'sender_email_field' => function ($formData) { // Use the form data directly here, or for example fetch the current // logged in account and get the email that way. return $formData->getEmail(); }, 'sender_nickname_field' => 'senderNickname', 'phone_field' => 'phone', 'message_field' => 'message' ]);
路线图
以下是一些很棒的选择
- Cleantalk 返回拒绝提交的原因。这可以记录下来。