How can I check if request was a POST or GET request in Symfony2 or Symfony3
Asked Answered
B

6

51

I just wondered if there is a very easy way (best: a simple $this->container->isGet() I can call) to determine whether the request is a $_POST or a $_GET request.

According to the docs,

A Request object holds information about the client request. This information can be accessed via several public properties:

  • request: equivalent of $_POST;
  • query: equivalent of $_GET ($request->query->get('name'));

But I won't be able to use if($request->request) or if($request->query) to check, because both are existing attributes in the Request class.

So I was wondering of Symfony offers something like the

$this->container->isGet();
// or isQuery() or isPost() or isRequest();

mentioned above?

Bridoon answered 4/4, 2014 at 2:13 Comment(4)
you can do like $request->get('name').whatever request method is post or get..if u really want to determine request method you can add a "requirements: [_method: POST/GET/DELETE....]"Tachograph
You mean adding this line to the routing config? I do check restrict methods there and now I want to determine them in controller.Bridoon
if u check restrict methods in your route,then just use $request->get('name') in your controller,that's enoughTachograph
I am using one controller to handle both methods, e.g. as in forms (where you can use $form->isSubmitted() to check this. But in my case I don't have/use a form.Bridoon
M
85

If you want to do it in controller,

$this->getRequest()->isMethod('GET');

or in your model (service), inject or pass the Request object to your model first, then do the same like the above.

Edit: for Symfony 3 use this code

if ($request->isMethod('post')) {
    // your code
}
Mountaintop answered 4/4, 2014 at 3:22 Comment(2)
Yeah thanks, also found the answer here https://mcmap.net/q/354559/-how-to-get-request-type-master-sub-in-symfony2-controllerBridoon
As MHakvoort also mentioned in his answer, $this->getRequest() is deprecated as of Symfony 2.4.Whoops
G
46

Or this:

public function myAction(Request $request)
{
    if ($request->isMethod('POST')) {

    }
}
Glee answered 14/3, 2015 at 16:5 Comment(1)
Have updated the question name so people can find it if searchingGlee
F
6

Or this:

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();

    if ($request->getMethod() === 'POST' ) {
}
Fernald answered 25/11, 2016 at 16:23 Comment(2)
this is most likely ok for slim but Symfony best practice is to use dependency injection. However, your solution still works in Symfony 5.Neolamarckism
thumbs up for this savior! it works in Symfony 6.1 too.Files
T
4

Since the answer suggested to use getRequest() which is now deprecated, You can do it by this:

$this->get('request')->getMethod() == 'POST'
Tester answered 28/1, 2015 at 11:30 Comment(1)
This is also deprecated. In controller, you should only use the typehinted $request object. For example, $request->getMethod() == 'POST'.Thigmotropism
B
2

In addition - if you prefer using constants:

if ($request->isMethod(Request::METHOD_POST)) {}

See the Request class:

namespace Symfony\Component\HttpFoundation;

class Request
{
    public const METHOD_HEAD = 'HEAD';
    public const METHOD_GET = 'GET';
    public const METHOD_POST = 'POST';
    public const METHOD_PUT = 'PUT';
    public const METHOD_PATCH = 'PATCH';
    public const METHOD_DELETE = 'DELETE';
Brassy answered 28/6, 2022 at 15:31 Comment(0)
H
0

You could do:

if($this->request->getRealMethod() == 'post') {
    // is post
}

if($this->request->getRealMethod() == 'get') {
    // is get
}

Just read a bit about request object on Symfony API page.

Hans answered 21/4, 2017 at 1:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.