36

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

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

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.*
    }
}
  • what about $_GET ? – zhartaunik May 30 '15 at 16:16
  • 3
    Same way. Just use getParams instead of getPost – Marius May 30 '15 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 '15 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 '16 at 3:45
  • 1
    @JeroenVermeulen-MageHost there was no MEQP2 standard 2.5 years ago when I wrote this. – Marius Dec 31 '17 at 7:24
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);
    }
}
2

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();
    }
}
  • 2
    In the template, we don't need to re-declare the request variable. We have already : $this->_request – Khoa TruongDinh Nov 9 '17 at 4:21
16

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();
    }
}
  • My \Magento\Framework\App\RequestInterface does not have a method getPost(), are you sure about this? – peedee Aug 22 '16 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 '16 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 ? – Kirti Nariya Nov 6 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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