1

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.

2
  • why not use: {% set action = path('_contact_form_post') ~ '#myhash' %} and use the freshly set action variable among your options? 'action': {{ action }} Commented Feb 24, 2015 at 17:06
  • This works, thnx. I don't see any other answers, so you can post it as an answer. Commented Feb 27, 2015 at 13:03

1 Answer 1

3

use:

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

and use the freshly set action variable among your options?

'action': {{ action }}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.