Installation:
composer require c975l/contactform-bundle
Enable Routes:
Add to config/routes.yaml:
c975_l_contact_form:
resource: "@c975LContactFormBundle/"
type: attribute
prefix: /
Twig Extension:
Ensure Twig\Extensions\TextExtension is enabled in config/packages/twig_extensions.yaml (Symfony 4+ auto-configures this; verify if using custom Twig setup).
First Use Case: Render the form in a Twig template:
{{ render(controller('c975LContactFormBundle:Default:contact')) }}
This displays a pre-configured contact form with honeypot spam protection and auto-filled user data (if logged in).
Form Rendering:
render(controller()) in Twig for the default form.templates/default/contact.html.twig) to customize UI while preserving functionality.Event-Driven Customization:
// config/services.yaml
App\EventSubscriber\ContactFormSubscriber:
tags:
- { name: kernel.event_subscriber }
use c975L\ContactFormBundle\Event\ContactFormEvent;
class ContactFormSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
ContactFormEvent::FORM_SUBMIT => 'onFormSubmit',
];
}
public function onFormSubmit(ContactFormEvent $event) {
$event->setRecipient('custom@example.com');
}
}
Multi-Recipient Emails:
ContactFormEvent::EMAIL_SEND to dynamically set recipients (e.g., route to a user’s email via a service).Spam Protection:
config/packages/c975_l_contact_form.yaml:
c975_l_contact_form:
delay: 3000 # milliseconds
Localization:
prefix/defaults in routes.yaml).c975LContactFormBundle\Form\ContactType) by creating a custom type and overriding the bundle’s service.emails/contact.txt.twig for custom email content.csrf_token() is not overridden.Outdated Codebase:
symfony/form, symfony/mailer) if critical issues arise.Twig Extension Dependency:
Twig\Extensions\TextExtension is missing, the bundle may fail silently. Verify with:
php bin/console debug:container twig.text_extension
config/packages/twig.yaml:
twig:
extensions:
- Twig\Extensions\TextExtension
Route Conflicts:
prefix: / in routes.yaml may clash with other bundles. Use a unique prefix (e.g., /contact) or namespace routes.Event Dispatching:
FORM_SUBMIT fire after validation but before email sending. Use EMAIL_SEND to modify the email object (e.g., attachments, subject).Honeypot Visibility:
display: none). If spam persists, inspect the rendered HTML for visibility issues.Form Submission Issues:
/_profiler) for validation errors or event subscriber exceptions.APP_DEBUG=true) and inspect the ContactFormEvent object in subscribers.Email Not Sending:
config/packages/mailer.yaml).Custom Form Fields:
contact_type:
# config/services.yaml
app.contact_form.type:
class: App\Form\CustomContactType
tags: [form.type]
arguments: ['c975LContactFormBundle\Form\ContactType']
App\Form\CustomContactType extending the original type.Dynamic Recipients:
ContactFormEvent::EMAIL_SEND event to fetch recipients from a database:
$recipient = $this->userRepository->findOneBy(['id' => $event->getData()->getUserId()]);
$event->setRecipient($recipient->getEmail());
Attachment Support:
emails/contact.txt.twig) to include attachments:
{% for attachment in event.attachment %}
{{ attachment.getContent() }}
{% endfor %}
EMAIL_SEND event:
$event->addAttachmentFromPath('/path/to/file.pdf');
Logging:
use Psr\Log\LoggerInterface;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function onFormSubmit(ContactFormEvent $event) {
$this->logger->info('Form submitted', ['data' => $event->getData()->toArray()]);
}
How can I help you explore Laravel packages today?