Pregunta

I want to declare both methods GET and POST for a same route in my routing.yml.

According the documentation it's possible with annotations like that :

/**
 * @Route("/edit/{id}")
 * @Method({"GET", "POST"})
 */

But how to YAML ? I tried different things :

contact_envoi:
    pattern:  /contact-envoi
    defaults: { _controller: AcmeDemoBundle:Default:contactEnvoi }
    requirements:
        sf_method: ['get','post']

and

...
    requirements:
        _method: { 'GET', 'POST' }

but it still doesn't work ... Please help, I found nothing in the documentation about that.

¿Fue útil?

Solución

Thanks to Touki for his comment, it works!

I had to declare twice the same URL on two separate shares and each with their own method as explained here for Symfony 2.1 and here for Symfony 2.2.

contact:
    path:     /contact
    defaults: { _controller: AcmeDemoBundle:Main:contact }
    methods:  [GET]

contact_process:
    path:     /contact
    defaults: { _controller: AcmeDemoBundle:Main:contactProcess }
    methods:  [POST]

Otros consejos

You can get the same route with the GET and POST methods.

contact:
path:     /contact
defaults: { _controller: AcmeDemoBundle:Main:contact }
methods:  ['GET','POST']

Then manage in your controller the method used.

public function contactAction(Request $request)
{
    if ('POST' === $request->getMethod()) {
        ..
    }
}

just remove the methods

contact:
  path:     /contact
  defaults: { _controller: AcmeDemoBundle:Main:contact }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top