By kim.pepper on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
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
Documentation contradicts this change notice
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$_POSTarray:how can i send the form parameter using Request
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
After submit I need to send the form parameters to controller using Request method
controller
For those who arrives here
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:Instead, recommends:
So probably
Drupal::service('request_stack')method should be invoked to require a properRequestStackobject and proceed with processing that by the use one of its getter methods.