All PHP programmers are familiar with the superglobal variables such as $_GET, $_REQUEST, $_POST, but their direct use is not allowed according to Magento2 code standards. So don't use them in your Magento 2 projects, especially if you want to pass the code Technical Review on the Magento Marketplace.
So what methods should be used?
To get data from the query, use the following methods:
getParam($key, $default = null);
getParams();
getPostParam($key, $default = null);
getPost();
of the \Magento\Framework\App\RequestInterface class, for example:
protected $request;
public function __construct(
\Magento\Framework\App\RequestInterface $request,
....//інші параметри вашого класу
) {
$this->request = $request;
...//інший код конструктора
}
public function example() {
// $data = $_REQUEST;
$data = $this->request->getParams();
// $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
$id = $this->request->getParam('id');
// $name = isset($_REQUEST['name']) ? $_REQUEST['name'] : 'петро';
$name = $this->request->getParam('name', 'петро');
// $postData = $_POST;
$postData = $this->request->getPost();
// $password = isset($_POST['password']) ? $_REQUEST['password'] : null;
$name = $this->request->getPostValue('password');
// $storeId = isset($_POST['store_id']) ? $_REQUEST['store_id'] : 1;
$storeId = $this->request->getPostValue('store_id', 1);
}
If you have experience with ZendFramework, then you should be familiar with these methods.
Important! To get a query object in a controller or block class, do not declare any additional dependency in the constructor. The public method getRequest() is available to you.