Installation:
composer require dankempster/contact-bundle:1.0.*
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
return [
// ...
\FrequenceWeb\Bundle\ContactBundle\FrequenceWebContactBundle::class,
];
Basic Configuration (config/packages/frequence_web_contact.yaml):
frequence_web_contact:
send_mails: true
to: "contact@example.com"
from: "noreply@example.com"
subject: "contact.message.new" # Requires translation
Routing:
Import the default routes in config/routes.yaml:
frequence_web_contact:
resource: "@FrequenceWebContactBundle/Resources/config/routing.xml"
Now access /contact for the form and submission.
First Use Case:
/contact to render the form.ContactFormSubmittedEvent (if send_mails: true).to email) for the submitted message.Form Handling:
@FrequenceWebContact/contact/form.html.twig).{% extends '@FrequenceWebContact/contact/form.html.twig' %}
{% block contact_form_fields %}
{{ parent() }}
<!-- Add custom fields here -->
{% endblock %}
Event-Driven Logic:
ContactFormSubmittedEvent to extend behavior:
// src/EventListener/CustomContactListener.php
namespace App\EventListener;
use FrequenceWeb\Bundle\ContactBundle\Event\ContactFormSubmittedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomContactListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
ContactFormSubmittedEvent::class => 'onContactSubmitted',
];
}
public function onContactSubmitted(ContactFormSubmittedEvent $event)
{
$data = $event->getData();
// Add custom logic (e.g., log to database, trigger Slack)
}
}
services.yaml:
services:
App\EventListener\CustomContactListener:
tags:
- { name: kernel.event_subscriber }
Validation:
FrequenceWeb\Bundle\ContactBundle\Form\ContactType) by creating a custom type:
// src/Form/Type/CustomContactType.php
namespace App\Form\Type;
use FrequenceWeb\Bundle\ContactBundle\Form\ContactType as BaseContactType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class CustomContactType extends BaseContactType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('custom_field', TextType::class);
}
}
services.yaml:
services:
frequence_web_contact.form.type.contact:
class: App\Form\Type\CustomContactType
tags: ['form.type']
Translation:
config/packages/translation.yaml:
translation:
paths: ['%kernel.project_dir%/translations']
translations/messages.en.yaml:
contact.message.new: "New Contact Form Submission"
contact.flash.success: "Your message has been sent successfully!"
Event Dispatching:
ContactFormSubmittedEvent is only dispatched if send_mails: true.send_mails: false and manually dispatch it in a custom listener for the form submission.Configuration Overrides:
to and from emails must be set in config; null values will throw errors.frequence_web_contact.yaml) and placed in config/packages/.Template Overrides:
name, email, and message. If you rename these in your custom form type, update the Twig blocks accordingly:
{% block contact_form_name_field %}
{{ form_row(form.name) }} {# Custom field name #}
{% endblock %}
CSRF Protection:
{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}
Check Event Firing:
dump() in your event subscriber to verify the event is triggered:
public function onContactSubmitted(ContactFormSubmittedEvent $event)
{
dump($event->getData()); // Debug submitted data
}
Mail Configuration:
to and from emails are valid.mailer_transport in .env) is configured.EmailListener (check Symfony logs).Routing Conflicts:
/contact conflicts with another route, use _frequence_web_contact with a custom prefix in routing.yaml:
frequence_web_contact:
resource: "@FrequenceWebContactBundle/Resources/config/routing.xml"
prefix: "/support"
Custom Storage:
public function onContactSubmitted(ContactFormSubmittedEvent $event)
{
$entityManager = $this->container->get('doctrine')->getManager();
$contact = new Contact();
$contact->setData($event->getData());
$entityManager->persist($contact);
$entityManager->flush();
}
Dynamic Recipients:
EmailListener to set dynamic to addresses:
# services.yaml
services:
frequence_web_contact.listener.email:
class: App\EventListener\DynamicEmailListener
arguments: ['@mailer']
tags:
- { name: kernel.event_listener, event: ContactFormSubmittedEvent, method: onContactSubmitted }
Async Processing:
public function onContactSubmitted(ContactFormSubmittedEvent $event)
{
$this->messageBus->dispatch(new ProcessContactMessage($event->getData()));
}
How can I help you explore Laravel packages today?