BigJ
BigJ

Reputation: 2032

Add URL anchor when returning template in Symfony controller

I want to add a URL anchor when I return a view in my controller. Right now the url is www.mydomain.com/contact, but I want it to be www.mydomain.com/contact#myparameter

/**
 * @Route("/contact", name="_contact_form_post")
 * @Template("MeterHomeBundle:Default:index.html.twig")
 */
public function postContactFormAction(Request $request) {

    $form = $this->createForm(new Form\Contact());
    $form->handleRequest($request);

    // Do all sorts of stuff here...

    return array("contactForm" => $form);
}

I can't use generateUrl() because I need to return a template and add the "contactForm" variable to it.

The form in my template starts with this code:

{{ form_start(contactForm, {'method': 'POST', 'action': path('_contact_form_post')}) }}

Could I add the anchor here somehow?


Edit: not so pretty solution in dev mode:

It works when I use this in my template to start the form:

{{ form_start(contactForm, {'method': 'POST', 'action': '/mydomain.com/app_dev.php/contact#contact'}) }}

But that is not how I want it: I would need to change it for production.

Upvotes: 1

Views: 2272

Answers (1)

Debreczeni András
Debreczeni András

Reputation: 1678

use:

{% set action = path('_contact_form_post') ~ '#myhash' %}

and use the freshly set action variable among your options?

'action': {{ action }}

Upvotes: 3

Related Questions