How to get $_POST and $_GET parameters

Solutions

In a controller get the request with a type-hinted argument Request $request:

<?php

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;

class ExampleController extends ControllerBase {

  /**
   * Controller to return a POST or a GET parameter.
   */
  public function action(Request $request) {

    // get your POST parameter
    $foo = $request->request->get('foo');

    // or get your GET parameter
    $foo = $request->query->get('foo');

    // POST requests are not cached, but for GET you need a cache context
    return [
      '#markup' => $foo,
      '#cache' => ['contexts' => ['url.query_args:foo']],
    ];
  }

}

More info https://www.drupal.org/docs/8/api/routing-system/using-parameters-in-routes

From the change record:

$name = $_POST['name']; // form param

becomes

$name = \Drupal::request()->request->get('name'); // form param

Incidentally, for GET vars, you would use:

$query = \Drupal::request()->query->get('name');

Inject the RequestStack into your controller.

The current request contains the query attribute that in turn contains the GET parameters. request contains the POST parameters.

<?php

namespace Drupal\example_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * An example controller.
 */
class ExampleController extends ControllerBase {

  /**
   * @var Symfony\Component\HttpFoundation\RequestStack
   */
  private $requestStack;

  /**
   * Constructor.
   *
   * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
   */
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('request_stack')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function action() {
    // Get your GET parameter here.
    $this->requestStack->getCurrentRequest()->query->get('foo');
  }

}
Tags: Drupal 8

Similar questions

Drupal filters and $_GET arguments
DRUPAL 6 Hello everybody, can someone help me with this problem? I have a carousel (view) called 'heat_model_carousel' and this carousel displays a list of sauna's. SO, each sauna has a field called 'Model Type' - which can be : Basic, Advanced or Premium. Now, i added a block of links : <a href = '?model_type=Basic'></a> I just want to...
How to set a $_POST var value as default value for a textfield webform component?
I need to set a $_POST variable value as the default value for a textfield webform component using Webform 4 In the Default Value field I filled with %post[my$_PostVar] but is not working. update: it seems like Webform 4 lost the %get and %post tokens comment-6337368 any thoughts... @googletorp I installed the Availability Calendars module. This mo...
Grabbing $_POST data
A quick summary of where I stand right now: We have a low traffic site here that was redone in Drupal about 8 months ago, before I was in the picture. An external design firm sold the owners on the technology as a way to enable non-programmers to update site content easily. Unfortunately, they did it wrong - they built our theme in-place over the B...
$_POST returns empty value in custom service
created a custom service resource to catch some data from the user through post method. but while I am accessing the service ie http://dev.localhost:8082/rest/send_mail/retrive.json Query: curl -X POST -H "Accept-Encoding: gzip" -H "Content-Type: application/json; charset=utf-8" sending the data in POST is : { "nid" :"1834", "recipient" :"charubach...
How can I add a sub-array structure in my results? $_POST
I'm building a form. In that form I have a lot of checkboxes. Normally, It's possible to make an array of some checkboxes (with name attribute). Normally I would have this result with print_r($_POST); I tried to add the name attribute, but the $_POST takes the structure of $form. But how can I add a sub-array structure in my results? This is what I...
Getting $_POST empty in AJAX callback
I just want to send address information to drupal for check content existence through the AJAX. Request is sending successfully and callback function also executing. but I am not getting any data in $_POST array. here is my AJAX code. This is callback routing & function At where I am making mistake? is there any other way? Or is there any probl...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.