alexbridge/symfony-bundle-contact
Installation
composer require alexbridge/symfony-bundle-contact
Ensure your composer.json includes Symfony 2.3+ and SensioFrameworkExtraBundle.
Enable the Bundle
Add to app/AppKernel.php:
new Alexo\ContactBundle\ContactBundle(),
Configure Routes
Add to app/config/routing.yml:
contact:
resource: "@ContactBundle/Controller/"
type: annotation
prefix: /contact
Configure Email Recipients
Add to app/config/config.yml:
contact:
receiver_emails: ["admin@example.com", "support@example.com"]
First Use Case
Access /contact in your browser to render the default form. Submit it to test email delivery.
Form Customization Extend the default form by overriding the template:
{% extends '@Contact/contact.html.twig' %}
{% block contact_form %}
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.email) }}
{{ form_row(form.subject) }}
{{ form_row(form.message) }}
<button type="submit" class="btn btn-primary">Send</button>
{{ form_end(form) }}
{% endblock %}
Validation & Processing
Override the controller logic in src/Alexo/ContactBundle/Controller/ContactController.php:
public function sendAction(Request $request)
{
$form = $this->createForm(new ContactType());
$form->handleRequest($request);
if ($form->isValid()) {
$message = $form->getData();
$this->get('contact.mailer')->send($message);
$this->addFlash('success', 'Message sent!');
}
return $this->render('Contact/contact.html.twig', ['form' => $form->createView()]);
}
Email Templating
Customize the email template by overriding Resources/views/Mail/contact.txt.twig:
From: {{ form.email }} <{{ form.email }}>
Subject: {{ form.subject }}
Message:
{{ form.message }}
Sender: {{ form.name }}
CSRF & Security Ensure the form includes CSRF protection (default in Symfony forms). For additional security:
# app/config/security.yml
security:
access_control:
- { path: ^/contact, roles: ROLE_USER }
Localization Translate form labels/placeholders via translation files:
# app/Resources/translations/messages.en.yml
contact:
name: "Your Name"
email: "Your Email"
subject: "Subject"
message: "Message"
Missing Routes
Forgetting to register the contact route in routing.yml will result in a 404. Verify with:
php app/console debug:router | grep contact
Empty receiver_emails
If receiver_emails is empty in config, emails will fail silently. Validate with:
php app/console debug:config contact
Symfony Version Mismatch The bundle requires Symfony 2.3+. Using an older version (e.g., 2.1) will break autoloading. Check compatibility with:
composer why-not symfony/framework-bundle:2.3.*
Form Submission Issues If submissions aren’t processed, ensure:
ContactType form class is autoloaded.contact.mailer service is properly configured (check services.yml).Email Delivery Failures Test locally with a mailcatcher or configure a real SMTP server:
# app/config/config.yml
swiftmailer:
transport: gmail
username: your@gmail.com
password: yourpassword
Log Form Data Add a debug dump in the controller:
if ($form->isValid()) {
$data = $form->getData();
$this->get('logger')->debug('Contact form data:', ['data' => $data]);
}
Validate Email Configuration Use Symfony’s debug tool to inspect the mailer:
php app/console debug:container | grep swiftmailer
Override Services To replace the mailer service (e.g., for testing):
# app/config/config_test.yml
services:
contact.mailer:
class: Symfony\Bridge\Twig\NodeVisitor\DebugByDefaultVisitor
arguments: ["@mailer"]
Custom Fields
Extend the ContactType form class:
// src/Alexo/ContactBundle/Form/ContactType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('phone', TextType::class, ['required' => false])
->add('terms', CheckboxType::class, ['mapped' => false]);
}
Dynamic Recipients Use a service to fetch recipients dynamically:
# app/config/services.yml
contact.recipient_resolver:
class: AppBundle\Service\DynamicRecipientResolver
arguments: ["@doctrine.orm.entity_manager"]
Then update the mailer service to use this resolver.
Event Listeners
Subscribe to the contact.send event to log or modify messages:
// src/AppBundle/EventListener/ContactListener.php
public function onContactSend(ContactEvent $event)
{
$this->get('logger')->info('Contact sent', ['data' => $event->getData()]);
}
Register in services.yml:
services:
app.contact_listener:
class: AppBundle\EventListener\ContactListener
tags:
- { name: kernel.event_listener, event: contact.send, method: onContactSend }
API Integration Expose the form as an API endpoint by creating a custom controller:
// src/AppBundle/Controller/ApiContactController.php
public function sendApiAction(Request $request)
{
$data = json_decode($request->getContent(), true);
$form = $this->createForm(new ContactType(), $data);
if ($form->isValid()) {
$this->get('contact.mailer')->send($form->getData());
return new JsonResponse(['status' => 'success']);
}
return new JsonResponse(['errors' => $form->getErrors(true)], 400);
}
How can I help you explore Laravel packages today?