48

In Magento 2 how to receive request array? $_POST and $_GET too as we did in Magento 1:

Mage::app()->getRequest()->getPost()
1
  • Can you specify in which class you want POST and GET data. Mar 30, 2019 at 7:33

6 Answers 6

101

If you are trying this from a controller that extends Magento\Framework\App\Action\Action you can get the request with $this->getRequest()->getPost().
If you are in a custom class, you need to inject the request in the constructor.

<?php
namespace Namespace\Module\Something;
class ClassName 
{
    protected $request;
    public function __construct(
       \Magento\Framework\App\RequestInterface $request
        ....//rest of parameters here
    ) {
       $this->request = $request;
       ...//rest of constructor here
    }
    public function getPost()
    {
        return $this->request->getPostValue();//in Magento 2.*
    }
}
10
  • what about $_GET ?
    – zhartaunik
    May 30, 2015 at 16:16
  • 4
    Same way. Just use getParams instead of getPost
    – Marius
    May 30, 2015 at 17:01
  • Thx, it works. I tried in PHPstorm with xDebug and look in window Watches. But I got empty array.
    – zhartaunik
    May 30, 2015 at 17:12
  • 1
    My class \Magento\Framework\App\Request\Http does not have a method getPost, are you sure about this?
    – peedee
    Aug 22, 2016 at 3:45
  • 1
    @JeroenVermeulen-MageHost there was no MEQP2 standard 2.5 years ago when I wrote this.
    – Marius
    Dec 31, 2017 at 7:24
24

Hi you can get it easily in models, blocks and controllers by using:

$this->getRequest()->getPost() 

Or add Magento\Framework\App\RequestInterface to the constructor parameters in your own classes:

<?php
namespace MyModuleNameSpace\MyModule\Block
use Magento\Framework\App\RequestInterface;

class MyClass
{
    /**
     * Request instance
     *
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $request;

    /**
     * @param RequestInterface $request
     */
    public function __construct(RequestInterface $request)
    {
        $this->request = $request;
    }

    public function getMyPostParams()
    {
        $postData = $this->request->getPost();
    }
}
3
  • My \Magento\Framework\App\RequestInterface does not have a method getPost(), are you sure about this?
    – peedee
    Aug 22, 2016 at 3:42
  • Did you try the code? Calling $this->getRequest()->getPost(); returns an Zend\Stdlib\Parameters object for me. Even in core, Magento uses plenty calls like this, with a parameter like for example in Magento\Sales\Controller\Adminhtml\Order\AddComment there is on line 31 a call: $data = $this->getRequest()->getPost('history');
    – Jacques
    Dec 21, 2016 at 12:15
  • @AmitBera I need help, Is there a way to call Magento\Catalog\Model\Product\Option\ReadHandler plugin class to only in get product details API ? Nov 6, 2019 at 7:26
3

This should work, just test it. Compare and see what is missing.

<?php
namespace MyModuleNameSpace\MyModule\Block
use Magento\Framework\App\RequestInterface;

class MyClass extends \Magento\Framework\View\Element\Template
{
    /**
     * Request instance
     *
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $request;

    /**
     * @param RequestInterface $request
     */
    public function __construct(
        RequestInterface $request,
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = [])
    {
        $this->request = $request;
        parent::__construct($context, $data);
    }

    public function getMyPostParams()
    {
        $postData = $this->request->getPost();
    }
}
1
  • 3
    In the template, we don't need to re-declare the request variable. We have already : $this->_request Nov 9, 2017 at 4:21
1
private $params = ['id', 'name'];

public function execute()
{
    $this->getPostParams();
}

private function getPostParams()
{
    foreach ($this->params as $k) 
    {
        $this->params[$k] = $this->getRequest->getParam($k);
    }
}
0

use data persister(\Magento\Framework\App\Request\DataPersistor) to get URL param in controller using Magento\Framework\App\Request\Http, than save and use it in collection class(where Magento\Framework\App\Request\Http is not working), take guidance from this How to pass data to UiComponent Area

$this->dataPersistor->set($key, $data)//in your controller

$this->dataPersistor->get($key)// in your collection

$this->dataPersistor->clear($key)in your collection once value used.
-1

namespace Vendor\Module\Controller\Adminhtml\ControllerName;

use Magento\Backend\App\Action;

use Magento\Backend\App\Action\Context;

use Magento\Framework\View\Result\PageFactory;

class ActionName extends Action

{

public function __construct(

    Action\Context $context,

) {

    parent::__construct($context);
}

public function execute()

{

    $data = $this->getRequest()->getPostValue();

}

}

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 27, 2022 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.