3

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.

Antoine Subit
  • 9,803
  • 4
  • 36
  • 52
  • 2
    As of Symfony 2.2, the way to declare method requirements has changed. From [`requirements._method:`](http://symfony.com/doc/2.1/book/routing.html#adding-http-method-requirements) to [`methods:`](http://symfony.com/doc/2.2/book/routing.html#adding-http-method-requirements). You need to use `contact_envoi.methods: ['GET', 'POST']` – Touki Apr 09 '14 at 09:51

3 Answers3

7

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]
Antoine Subit
  • 9,803
  • 4
  • 36
  • 52
5

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()) {
        ..
    }
}
Rémi Lavolée
  • 48
  • 1
  • 11
4

just remove the methods

contact:
  path:     /contact
  defaults: { _controller: AcmeDemoBundle:Main:contact }
Adnen Chouibi
  • 400
  • 4
  • 16