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?

timhc22
  • 7,213
  • 8
  • 48
  • 66
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
  • 1
    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....]" – Leo Silence Apr 04 '14 at 02:36
  • You mean adding this line to the routing config? I do check restrict methods there and now I want to determine them in controller. – Gottlieb Notschnabel Apr 04 '14 at 02:48
  • if u check restrict methods in your route,then just use $request->get('name') in your controller,that's enough – Leo Silence Apr 04 '14 at 02:53
  • 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. – Gottlieb Notschnabel Apr 04 '14 at 02:56

6 Answers6

84

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
}
Mohammed Zayan
  • 859
  • 11
  • 20
Nighon
  • 1,034
  • 11
  • 13
46

Or this:

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

    }
}
timhc22
  • 7,213
  • 8
  • 48
  • 66
6

Or this:

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();

    if ($request->getMethod() === 'POST' ) {
}
Azoel
  • 209
  • 6
  • 19
4

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

$this->get('request')->getMethod() == 'POST'
Matheno
  • 4,112
  • 6
  • 36
  • 53
  • 7
    This is also deprecated. In controller, you should only use the typehinted $request object. For example, $request->getMethod() == 'POST'. – Tomas Vysniauskas Feb 08 '16 at 13:42
1

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';
webDEVILopers
  • 1,886
  • 1
  • 21
  • 35
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.

HelpNeeder
  • 6,383
  • 24
  • 91
  • 155