c975l/contactform-bundle
Composer 安装命令:
composer require c975l/contactform-bundle
包简介
Symfony bundle that provides a fully-featured contact form with built-in spam protection, reCaptcha v3, rate limiting, event-driven customization, and multilingual support.
README 文档
README
Symfony bundle that provides a fully-featured contact form with built-in spam protection, reCaptcha v3, rate limiting, event-driven customization, and multilingual support.
Features
- Contact form at
/contact(or/{_locale}/contactfor multilingual apps) - Pre-fills name and email when a user is logged in
- Sends emails via Symfony Mailer (
TemplatedEmail) - Dispatches events to customize form behavior and email content
- Anti-spam: dynamic honeypot with randomized field names and labels per session
- Anti-spam: minimum submission delay check to reject bot submissions
- Anti-spam: reCaptcha v3 via
karser/KarserRecaptcha3Bundle - Rate limiting: optional limits by IP and by email address
- GDPR consent checkbox (configurable)
- Optional "receive a copy" checkbox for the sender
- Subject pre-fill via URL parameter (
?s=My+Subject) - Configuration managed via c975L/ConfigBundle
- Selectable as a navbar/footer menu item in c975L/SiteBundle (via
LinkableRouteProvider), with no dependency on SiteBundle itself
Requirements
- PHP >= 8.0
- Symfony >= 7.0
- c975L/ConfigBundle
- c975L/SiteBundle
- karser/karser-recaptcha3-bundle
Installation
Download
composer require c975l/contactform-bundle
Enable routes
Add the following to config/routes.yaml:
c975_l_contact_form: resource: "@c975LContactFormBundle/" type: attribute prefix: / # For multilingual websites: # prefix: /{_locale} # defaults: { _locale: '%locale%' } # requirements: # _locale: en|fr|es
Load configuration values
php bin/console c975l:config:load-all
Then use the ConfigBundle dashboard to set the values for each key.
Configure reCaptcha v3
Create your keys on Google reCaptcha and set them in your .env.local:
RECAPTCHA3_KEY=your_site_key RECAPTCHA3_SECRET=your_secret_key
Or store them via the ConfigBundle dashboard (recaptcha3-site-key and recaptcha3-secret-key).
Override the layout template
Create templates/bundles/c975LContactFormBundle/layout.html.twig and extend your own layout:
{% extends 'layout.html.twig' %}
{% set title = 'Contact' %}
{% block content %}
{% block contactform_content %}
{% endblock %}
{% endblock %}
The email templates are loaded from SiteBundle. Override them in templates/c975LSiteBundle/emails/.
Usage
The route name is contactform_display. Link to it from Twig:
{{ path('contactform_display') }}
Pre-filling the subject
Pass the s query parameter to pre-fill the subject field (rendered as read-only):
https://example.com/contact?s=My+Subject
Anti-bot delay and GDPR checkbox
Both are driven by two config keys shared with the rest of the c975L ecosystem (e.g. c975l/site-bundle's scaffolded registration/reset-password forms), so every public form agrees on one setting instead of one per bundle. Seeded by this bundle's own config/configs.json too, so they exist even without c975l/site-bundle installed:
site-form-delay(int, seconds, default3) - below this delay between displaying and submitting the form, the submission is treated as a bot and silently dropped.site-form-gdpr(bool, defaulttrue) - shows the mandatory GDPR consent checkbox.
Renamed from
contact-form-delay/contact-form-gdpr- see UPGRADE.md if you had customized either.
Rate limiting (optional)
If the following Symfony RateLimiter services are defined, they are automatically applied before any email is sent:
limiter.contact_form_by_iplimiter.contact_form_by_email
Example (config/packages/rate_limiter.yaml):
framework: rate_limiter: contact_form_by_ip: policy: sliding_window limit: 5 interval: '10 minutes' contact_form_by_email: policy: sliding_window limit: 3 interval: '10 minutes'
Honeypot and CSP
If you have disabled unsafe-inline for style-src in your Content Security Policy, add this rule to keep the honeypot hidden:
.sr-only { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
Events
Two events allow customization without modifying the bundle:
| Constant | Event name | Fired when |
|---|---|---|
ContactFormEvent::CREATE_FORM |
c975l_contactform.create.form |
The form is being built |
ContactFormEvent::SEND_FORM |
c975l_contactform.send.form |
The form has been submitted and validated |
Example: customize email on submission
namespace App\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use c975L\ContactFormBundle\Event\ContactFormEvent; class ContactFormSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ ContactFormEvent::SEND_FORM => 'onSendForm', ]; } public function onSendForm(ContactFormEvent $event): void { $subject = $event->getFormData()->getSubject(); if (str_contains((string) $subject, 'some-keyword')) { $event->setEmailData([ 'subject' => 'Custom email subject', 'bodyEmail' => 'emails/custom_contact.html.twig', 'bodyData' => [], ]); // Or abort sending with an error code: // $event->setError('error.user_not_found'); } } }
Override the redirect URL after submission
public function onCreateForm(ContactFormEvent $event): void { $event->getRequest()->getSession()->set('redirectUrl', 'https://example.com/thank-you'); }
If this bundle saves you development time, consider sponsoring via the Sponsor button at the top of the repository. Thank you!
c975l/contactform-bundle 适用场景与选型建议
c975l/contactform-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 896 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「contact」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 c975l/contactform-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 c975l/contactform-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 c975l/contactform-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
Bundle Symfony DaplosBundle
Provides a Data Grid tables for your Symfony project.
统计信息
- 总下载量: 896
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-05