Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Bundle Contact Laravel Package

alexbridge/symfony-bundle-contact

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require alexbridge/symfony-bundle-contact
    

    Ensure your composer.json includes Symfony 2.3+ and SensioFrameworkExtraBundle.

  2. Enable the Bundle Add to app/AppKernel.php:

    new Alexo\ContactBundle\ContactBundle(),
    
  3. Configure Routes Add to app/config/routing.yml:

    contact:
        resource: "@ContactBundle/Controller/"
        type:     annotation
        prefix:   /contact
    
  4. Configure Email Recipients Add to app/config/config.yml:

    contact:
        receiver_emails: ["admin@example.com", "support@example.com"]
    
  5. First Use Case Access /contact in your browser to render the default form. Submit it to test email delivery.


Implementation Patterns

Workflow Integration

  1. 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 %}
    
  2. 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()]);
    }
    
  3. 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 }}
    
  4. 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 }
    
  5. 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"
    

Gotchas and Tips

Pitfalls

  1. 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
    
  2. Empty receiver_emails If receiver_emails is empty in config, emails will fail silently. Validate with:

    php app/console debug:config contact
    
  3. 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.*
    
  4. Form Submission Issues If submissions aren’t processed, ensure:

    • The ContactType form class is autoloaded.
    • The contact.mailer service is properly configured (check services.yml).
  5. 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
    

Debugging Tips

  1. 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]);
    }
    
  2. Validate Email Configuration Use Symfony’s debug tool to inspect the mailer:

    php app/console debug:container | grep swiftmailer
    
  3. 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"]
    

Extension Points

  1. 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]);
    }
    
  2. 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.

  3. 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 }
    
  4. 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);
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle