Magento 2 Get Post Data in Observer


Magento 2 Get Post Data in Observer– We can get the form post/get value in observer using request interface in constructor. Here in this tutorial, we are going to explain how you can get the form post data in observer in Magento 2.


Magento 2 Get Post Data in Observer Example

First of all you need to inject \Magento\Framework\App\RequestInterface $request in class constructor then you can get the form post value in observer simply as below-

Example:

<?php

namespace Test\Helloworld\Observer;


use Magento\Framework\Event\ObserverInterface;

class MyObserver implements ObserverInterface
{
 public function __construct(
    \Magento\Framework\App\RequestInterface $request
)
{
    $this->_request = $request;
}
 public function execute(\Magento\Framework\Event\Observer $observer)

 {
  $postData = $this->_request->getPost();
  print_r($postData);
  }

}

On the same way you can use request/post or get method to get the form post data.


Add Comment

📖 Read More


    Advertisements