webfactory/newsletter-registration-bundle

Newsletter-Registration 的 Symfony-Bundle

dev-master 2024-08-27 11:36 UTC

This package is auto-updated.

Last update: 2024-08-27 11:36:44 UTC


README

这个 Symfony Bundle 提供了一个具有隐私保护数据避免注意力的新闻通讯注册模板

  • 仅通过电子邮件地址注册(这也是提高互动率的一个低门槛)
  • 直到新闻通讯接收者验证他们的电子邮件地址(双确认)之前,不会保存任何个人信息(如电子邮件地址)
  • 待确认的流程将在可配置的时间后删除(默认:72小时)
  • 故意含糊其辞的消息,以防止泄露用户元信息(例如,如果某些电子邮件地址已注册)

为了减少垃圾邮件的数量,以下是一些实施的想法

  • 注册表单有一个简单的蜜罐字段
  • 确认电子邮件不包含注册表单中输入的数据,使其对垃圾邮件发送者不具吸引力
  • 确认电子邮件只能在可配置的时间间隔内发送一次(默认:1小时)
  • 确认电子邮件包含一个“阻止此电子邮件地址”链接(默认:30天)

最后,这个 Bundle 试图对开发者友好

  • 注册可以作为独立页面或部分视图嵌入
  • 根据不同新闻通讯的数量,注册和编辑表单具有新闻通讯选择或无干扰元素(单份新闻通讯的复选框可能很愚蠢)
  • 由于其小接口、Doctrine 接口映射和服务替换,因此可以高度自定义

安装

composer req webfactory/newsletter-registration-bundle

src/bundles.php 中激活

<?php

return [
    // ...
    Webfactory\NewsletterRegistrationBundle\WebfactoryNewsletterRegistrationBundle::class => ['all' => true],
];

在您的项目中实现所有 src/Entity/*Interface.php。如果您不介意有偏见的命名空间,最简单的方法是复制模板

mkdir src/AppBundle/Newsletter
cp vendor/webfactory/newsletter-registration-bundle/app-class-templates/* src/AppBundle/Newsletter/*

如果您想自己实现接口,可以像上面的模板一样扩展相应的抽象类,并添加类级别的 Doctrine ORM 注解(在抽象类中找到它们的模板)。有关自定义信息,请参阅下面的“自定义”部分。

无论如何,配置 Doctrine 的接口映射以处理您的自定义实体类

// config.yml

doctrine:
    orm:
        resolve_target_entities:
            \Webfactory\NewsletterRegistrationBundle\Entity\NewsletterInterface: '\AppBundle\Entity\Newsletter'

侧注:上面的模板和示例假设您希望在 AppBundle 中的 Newsletter 目录中保留您的 Newsletter 类。如果您这样做,您可能需要配置 Doctrine 来加载实体

// config.yml

doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    NewsletterRegistrationBundle:
                        type: annotation
                        prefix: AppBundle\Newsletter\Entity\
                        dir: "%kernel.root_dir%/AppBundle/Newsletter/Entity/"
                        is_bundle: false

更新您的数据库模式,例如使用迁移。

配置 Bundle

// config.yml

parameters:
  webfactory.newsletter_registration.email_sender_address: 'newsletter@example.com'
  webfactory.newsletter_registration.secret: 'your-secret' # do not use Symfony's %secret%!
  webfactory.newsletter_registration.time_limit_for_opt_in_in_hours: 72 # default value
  webfactory.newsletter_registration.minimal_interval_between_op_in_emails_in_hours: 1 # default value
  webfactory.newsletter_registration.block_email_address_duration_in_days: 30 # default value

在路由中包含 RegistrationController

// routing.yml

newsletter:
    prefix: /newsletter
    type: annotation
    resource: '@WebfactoryNewsletterRegistrationBundle/Controller/RegistrationController.php'

RegistrationController 的构造函数中注入了一些接口。将这些接口与其自己的实现别名

// src/services.yml

services:
  AppBundle\Newsletter\Entity\NewsletterRepository:
    factory:
      - '@doctrine.orm.entity_manager'
      - 'getRepository'
    arguments:
      - 'AppBundle\Entity\Newsletter'

  Webfactory\NewsletterRegistrationBundle\Entity\NewsletterRepositoryInterface:
    alias: 'AppBundle\Newsletter\Entity\NewsletterRepository'

  AppBundle\Newsletter\Entity\PendingOptInRepository:
    factory:
      - '@doctrine.orm.entity_manager'
      - 'getRepository'
    arguments:
      - 'AppBundle\Entity\PendingOptIn'

  Webfactory\NewsletterRegistrationBundle\Entity\PendingOptInRepositoryInterface:
    alias: 'AppBundle\Newsletter\Entity\PendingOptInRepository'

  AppBundle\Newsletter\Entity\RecipientRepository:
    factory:
      - '@doctrine.orm.entity_manager'
      - 'getRepository'
    arguments:
      - 'AppBundle\Entity\Recipient'

  Webfactory\NewsletterRegistrationBundle\Entity\RecipientRepositoryInterface:
    alias: 'AppBundle\Newsletter\Entity\RecipientRepository'

删除过时的数据

bin/console newsletter-registration:delete-outdated-pending-opt-ins
bin/console newsletter-registration:delete-outdated-blocked-email-addresses

自定义

视图

使用 symfony 的常规模板覆盖机制

从以下开始

mkdir src/Resources/WebfactoryNewsletterRegistrationBundle -p
cp -r vendor/webfactory/newsletter-registration-bundle/src/Resources/views src/Resources/WebfactoryNewsletterRegistrationBundle 

翻译

使用 翻译组件的覆盖机制

如果您添加了新语言或修复了错误,请考虑通过拉取请求进行贡献。

添加字段

  • 通过 表单类型扩展 扩展 StartRegistration Type。
  • 将新字段添加到您的实体中(例如,上面的示例中的 AppBundle\Entity\PendingOptInAppBundle\Entity\Recipient)。也许您还想扩展它们各自的存储库。
  • 实现 Webfactory\NewsletterRegistrationBundle\Entity\PendingOptInFactoryInterfaceWebfactory\NewsletterRegistrationBundle\Entity\RecpientFactoryInterface,因为它们负责从相应的表单数据创建您的实体。将这些接口与其实现别名,例如
    // services.yml
    services:
      Webfactory\NewsletterRegistrationBundle\Entity\PendingOptInFactoryInterface:
          alias: 'App\Newsletter\Entity\PendingOptInFactory'

逻辑

如果您可以将您的修改锁定在 StartRegistrationConfirmRegistrationEditRegistrationDeleteRegistration 任务上,那么您可能最好实现各自的接口版本(可能扩展任务类)并将接口服务别名为它们。

为了更大的灵活性,您可以替换 RegistrationController 为您自己的,例如:

<?php

namespace AppBundle\Newsletter;

class Controller extends \Webfactory\NewsletterRegistrationBundle\Controller\Controller
{
    // ...
}

别忘了相应地配置您的路由和服务。