rmzamora/contact-bundle

ihqs/contact-bundle 的分支版本 - symfony 的联系人表单集成

dev-master 2014-10-27 04:46 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:32:03 UTC


README

提供基于观察者模式的简单但强大的联系人表单。将任何监听器连接到您的联系表单提交

待办事项

  • 添加 mongodb 配置文件
  • 添加单元测试
  • 注释方法和属性
  • 清理

(与 GithubBundle 相同,我们对此方法表示歉意,但我们很快会纠正这一点)。

特性

  • 由于通用存储库,与 Doctrine ORM ODM 兼容
  • 模型可根据需要扩展
  • 为更多灵活性而设计的观察者模式
  • 每个类都是可定制的

安装

使用子模块 如果您更愿意使用 git 子模块,请运行以下命令

$ git submodule add git://github.com/rz/ContactBundle.git    src/Rz/ContactBundle

使用 vendors 脚本

在您的 deps 文件中添加以下行

[RzContactBundle]
    git=git://github.com/rz/ContactBundle.git
    target=bundles/Rz/ContactBundle

如果您想连接电子邮件监听器,安装 SwitfMailer 并进行配置

$ git submodule add git://github.com/swiftmailer.git    src/vendor/swiftmailer

或通过 deps 文件

[swiftmailer]
    git=http://github.com/swiftmailer/swiftmailer.git
    version=v4.1.2

在您的配置中添加

swiftmailer.config:
    transport:  smtp
    encryption: ssl
    auth_mode:  login
    host:       domain.tld
    username:   name@domain.tld
    password:   your_secret

将 Rz 命名空间添加到您的自动加载器中

// app/autoload.php
$loader->registerNamespaces(array(
    'Rz' => __DIR__.'/../vendor/bundles',
    // your other namespaces
);

将 ContactBundle 添加到您的应用程序内核中

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Rz\ContactBundle\RzContactBundle(),
        // ...
    );
}

更新您的模式

app/console doctrine:schema:update --force

将表单添加到您的模板中

在您的模板中,您只需添加(如果您正在使用 Twig)

{% render "RzContactBundle:Contact:form" with { 'method': app.request.method }, { 'query': app.request.request.all } %}

从 Symfony 2.0.13 版本开始,您必须使用 "query" 参数发送您的表单 "POST" 数据。这是因为 "render" 标签正在作为子请求处理。从该版本开始,所有子请求都强制使用 "GET" 方法,因此如果错过它,您的嵌入式 ContactController 将丢失所有 "POST" 数据。此外,您应显式指定请求方法参数,以便嵌入式 ContactController 能够识别您的表单已被提交。

配置

在您的 app/config.yml(假设您正在使用 YAML 配置文件)中

rz_contact:
    contact:
        form:
            type:               rz_contact_contact
            handler:            rz_contact.contact.form.handler.default
            name:               rz_contact_contact_form
            validation_groups:  [Contact]
    form: ~                                 # (optional) class managing the contact form
    model: ~                                # (optional) class managing the model
    connectors:                             # the list of "listeners" (or connectors here)
        email:                              # connecting the email listener
            recipients: contact@rz.net    # giving him the recipient(s) email(s)
        database:                           # connecting the database listener
            db_driver: orm                  # giving him the database driver
        file: ~                             # connecting the file logger listener

如果您只想有一个简单的联系人表单,只发送电子邮件到您的联系人邮箱,则只需将以下内容添加到您的配置文件中

rz_contact:
    connectors:
        email:
            recipients: cont@ct.me

如果您想使用自定义模板/视图而不是默认视图(RzContactBundle:Contact:form.html.twig),您可以添加带有您视图名称的视图参数。示例

rz_contact:
    contact:
        form:
			view:               CompanyExampleBundle:Contact:form.html.twig

使用 Akismet 进行垃圾邮件检测

如果您不想检查垃圾邮件,则可以跳过此步骤,默认情况下不会检测垃圾邮件。安装 OrnicarAkismentBundle 并正确配置它。有关更多信息,请参阅文档。

在服务容器中定义服务

rz_contact:
    spam_detector:
        service: ornicar_akismet

现在所有您的联系人请求都将由 Akismet 检查。

如果您想实现自己的检测器,只需设置检测器的类

rz_contact:
    spam_detector:
        class: My\Bundle\MyDetector

请注意,该类应实现 SpamDetectorInterface

或如果您想创建全新的服务

rz_contact:
    spam_detector:
        service: my_detector_service