In Magento 2 how to receive request array? $_POST
and $_GET
too as we did in Magento 1:
Mage::app()->getRequest()->getPost()
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIf 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.*
}
}
\Magento\Framework\App\Request\Http
does not have a method getPost
, are you sure about this?
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();
}
}
\Magento\Framework\App\RequestInterface
does not have a method getPost()
, are you sure about this?
$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');
Magento\Catalog\Model\Product\Option\ReadHandler
plugin class to only in get product details API ?
Nov 6, 2019 at 7:26
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();
}
}
$this->_request
Nov 9, 2017 at 4:21
private $params = ['id', 'name'];
public function execute()
{
$this->getPostParams();
}
private function getPostParams()
{
foreach ($this->params as $k)
{
$this->params[$k] = $this->getRequest->getParam($k);
}
}
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.
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();
}
}