Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

In Drupal 7, you would access GET, POST, SERVER and COOKIE variables using the PHP super-globals $_GET, $_POST, $_SERVER and $_COOKIE.

Drupal 7:

$query = $_GET['q']; // query string param
$myparam = $_POST['myparam']; // form param
$request_method = $_SERVER['REQUEST_METHOD']; // server variable
$mycookie = $_COOKIE['mycookie']; // cookie

In Drupal 8, you need to use the Symfony Request object.

Drupal 8:

$query = \Drupal::request()->query->get('q'); // query string param
$name = \Drupal::request()->request->get('name'); // form param
$request_method = \Drupal::request()->server->get('REQUEST_METHOD'); // server variable
$mycookie = \Drupal::request()->cookies->get('mycookie'); // cookie

More information on the Request object is available on the Symfony2 documentation pages. http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html

Impacts: 
Module developers

Comments

kenficara’s picture

According to the documentation for Request::get, this method "should not be used in controllers" because it's slow. The recommendation is to go straight to the request object itself. So to get the equivalent of the $_POST array:

$post_params = \Drupal::request()->request->all();
VIJOY.T.ABRAHAM’s picture

I have a custom module which contain form I need to send the form parameters to another page using httpfoundation Request. How can I set parameter in request ?

form

public function buildForm(array $form, FormStateInterface $form_state) {


   $form['message1'] = array(
      '#markup' => $this->t('Current Details'),
    );
 
        
      
	$form['OrderId'] = array(
	      '#type' => 'textfield',
	      '#title' => t('REFERENCE ID:'),
	      '#required' => TRUE,
	    );
      

	$form['amount'] = array(
	      '#type' => 'textfield',
	      '#title' => t('TRANSACTION AMOUNT:'),
	      '#required' => TRUE,
	    );

        $form['Back'] = array(
	      '#type' => 'submit',
	      '#value' => t('Back'),
	    );

       
	$form['submit']= array(
	      '#type' => 'submit',
	      '#value' => t('Pay'),
             
	    );
           

	 return $form;
	}

function submitForm(array &$form, FormStateInterface $form_state){
}

After submit I need to send the form parameters to controller using Request method

controller

class Uniview8WorldlineController extends ControllerBase {

  public function content()
    {

        
    }
}
baluertl’s picture

For those who arrives here seeking answer: as @kenficara pointed out, since 2013 api.drupal.org documentation has went ahead further, indeed. In core version 8.8 the Drupal::request() method states about itself:

"Note: The use of this wrapper in particular is especially discouraged."

Instead, recommends:

"better to register an object with the Service Container [...]"

So probably Drupal::service('request_stack') method should be invoked to require a proper RequestStack object and proceed with processing that by the use one of its getter methods.