c975l/contactform-bundle

管理简单联系表单的包


README

ContactFormBundle可以执行以下操作

  • 显示一个联系网站的表单,
  • 如果用户已登录,则预填数据,
  • 派发事件以修改表单/电子邮件,
  • 通过c975LEmailBundle发送电子邮件,作为c975LEmailBundle提供将电子邮件保存到数据库的可能性,此包有一个选项可以通过此包不这样做,
  • 将副本发送到提供的电子邮件,
  • 允许将电子邮件发送给其他用户,相关于您的应用规范,即联系另一个用户而无需提供其电子邮件。这是通过事件派发(见下文)实现的,
  • 提供蜜罐和提交前的延迟,以防止垃圾邮件且无需请求验证码(见下文),

ContactFormBundle专用网页.

ContactFormBundle API文档.

包安装

步骤 1: 下载包

v5.x 与 Symfony 6.x 兼容。 使用 v3|4.x 用于 Symfony 4.x 使用 v2.x 用于 Symfony 3.x 使用 Composer 安装库

    composer require c975l/contactform-bundle

步骤 2: 配置包

检查依赖项以进行其配置

c975LContactFormBundle使用c975L/ConfigBundle来管理配置参数。使用路由"/contact/config"以及适当的用户角色来修改它们。

步骤 3: 声明Twig\Extensions\TextExtension

您必须在您的/config/pacakes/twig_extensions.yaml中允许Twig\Extensions\TextExtension

步骤 4: 启用路由

然后,通过将它们添加到您的项目的/config/routes.yaml文件中,启用路由。

c975_l_contact_form:
    resource: "@c975LContactFormBundle/Controller/"
    type:     annotation
    prefix:   /
    #Multilingual website use the following
    #prefix: /{_locale}
    #defaults:   { _locale: '%locale%' }
    #requirements:
    #    _locale: en|fr|es

步骤 5: 覆盖模板

强烈建议使用从第三方包覆盖模板的功能来与您的站点完全集成。

为此,只需在您的应用程序中创建以下结构 /templates/bundles/c975LContactFormBundle/,然后复制其中的layout.html.twig文件,以覆盖现有的包文件。

layout.html.twig中,它将主要扩展您的布局并定义特定的变量,例如

{% extends 'layout.html.twig' %}

{# Defines specific variables #}
{% set title = 'Contact' %}

{% block content %}
    {% block contactform_content %}
    {% endblock %}
{% endblock %}

用于发送电子邮件的模板是c975LEmailBundle的模板。在/templates/c975LEmailBundle/emails/layout.html.twig中覆盖它。

如何使用

路由名称是contactform_display,因此您可以通过{{ path('contactform_display') }}在Twig中添加链接。

URL路径是/contact/{_locale}/contact,因此只需访问http://example.com/contacthttp://example.com/en/contact来显示表单。

您可以使用URL参数s设置主题,例如http://example.com/contact?s=Subject,该字段将在表单中只读,但当然,它可以通过URL进行更改。该值将被清理并作为(subject)传递给表单,以便能够根据此值更改标题和/或信息文本,例如

{% if 'Subject' in subject %}
    {# Do some stuff #}
{% endif %}

蜜罐和延迟以防止垃圾邮件

为了避免ContactFormBundle作为发送垃圾邮件的入口点,字段username是一个蜜罐。它仅对机器人显示,并通过CSS对正常用户隐藏。如果它被填写,则不是用户,而是机器人。还有一个用于提交表单的延迟的测试。如果表单在定义的延迟之前提交,则可能不是由人类填写。

对于这两种情况,ContactFormBundle 将会像邮件已发送一样操作,但实际上并不是这样。

更改 infoText

您可以通过以下代码在您的覆盖模板 /templates/c975LContactFormBundle/layout.html.twig 中更改在联系表单顶部显示的文本。

{% extends 'layout.html.twig' %}

{% set infoText = 'text.contact_info'|trans({'%site%': site}, 'contactForm') %}

{% if YOUR_CONDITION_IS_MET %}
    {% set infoText = 'YOUR_TEXT_TO_DISPLAY' %}
{% endif %}

{% block content %}
    {% block contactform_content %}
    {% endblock %}
{% endblock %}

事件分发

禁用“接收副本”复选框

您可以通过捕获以下代码中的 CREATE_FORM 事件来禁用复选框,允许用户接收发送的电子邮件副本。这在例如,如果联系表单用于联系另一个用户,并且您想保留其电子邮件地址时非常有用。

namespace AppBundle\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use c975L\ContactFormBundle\Event\ContactFormEvent;

class ContactFormSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            ContactFormEvent::CREATE_FORM => 'createForm',
        );
    }

    public function createForm($event)
    {
        //Gets data
        $formData = $event->getFormData();
        $subject = $formData->getSubject();

        //For example, you can check if a string is present in the subject
        if (stripos($subject, 'THE_STRING_YOU_WANT_TO_MATCH_IN_THE_SUBJECT') === 0) {
            $event->setReceiveCopy(false);
        }
    }
}

在发送的电子邮件中设置特定数据

根据您的应用规格,您可以根据表单发送的数据(正文、主题等)设置特定的电子邮件数据。为此,您需要创建以下代码的监听器。

namespace AppBundle\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use c975L\ContactFormBundle\Event\ContactFormEvent;

class ContactFormSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            ContactFormEvent::SEND_FORM => 'sendForm',
        );
    }

    public function sendForm($event)
    {
        //Gets data
        $formData = $event->getFormData();
        $subject = $formData->getSubject();

        //For example, you can check if a string is present in the subject
        if (stripos($subject, 'THE_STRING_YOU_WANT_TO_MATCH_IN_THE_SUBJECT') === 0) {
            //Do the stuff...

            //Conditions to send email are met
            if (1 == 1) {
                //Defines data for email
                $bodyEmail = 'YOUR_EMAIL_TEMPLATE.html.twig';
                $bodyData = array(
                     //Any needed data for your template
                    );
                //The following array keys are mandatory, but you can set the other keys defined in c975L\EmailBundle
                $emailData = array(
                    'subject' => 'YOUR_EMAIL_SUBJECT',
                    'bodyData' => $bodyData,
                    'bodyEmail' => $bodyEmail,
                    );

                //Updates event
                $event->setEmailData($emailData);
            //Stop sending by setting an error code, it will create a flash including your error code
            } else {
                $event->setError('YOUR_ERROR_CODE');
            }
        }
    }
}

更新重定向 URL

您可以使用以下代码更新提交表单后要重定向到的 URL。

namespace AppBundle\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use c975L\ContactFormBundle\Event\ContactFormEvent;

class ContactFormSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            ContactFormEvent::CREATE_FORM => 'createForm',
        );
    }

    public function createForm($event)
    {
        //Updates url to redirect
        $event->getRequest()->getSession()->set('redirectUrl', 'https://example.com');
    }
}

如果这个项目 帮助您减少了开发时间,您可以通过顶部“赞助”按钮赞助我:(