In this blog, we will learn about different operations on request and response in magento2. First of all, let’s start with reading GET/POST data. In many cases, we need to read data passed using GET or POST method such as data passed from form. To process this data, we need to first read the data. The method is similar to magento 1.

To read data passed via get Method we simply use following two methods:

$this->getRequest()->getParams()

this will read all the get data but to read any specific data we use

$this->getRequest()->getParam('data');

To read data passed via POST method, we use following method:

$this->request->getPost()

this will read all the the data being passed via post. But if we want to read specific data then we will use

$this->getRequest()->getPost('data');

But, if you go through the magento2 core files, then you will find getPostvalue() method used instead of getPost(). It is defined in lib\internal\Magento\Framework\HTTP\PhpEnvironment\Request.php:

Although both the methods, getPost() & getPostValue() have almost similar functionality but there is slight difference in the format they return data. In magento2, it is supposed to be good practice to use getPostValue().

But when we use getPostValue() then we get a well structured array like: Array ( [data] => Array ( [title] => abc [status] => 1 [submit] => Save ) ) which is easy to manipulate. This is the reason, getPostValue is preferred over getPost() method.

then It get the getPost value from vendor\zendframework\zend-http\src\Request.php:

The above methods will work if you are trying this from a controller that extends Magento\Framework\App\Action\Action you can get the request. In other cases you need to inject request in the constructor as:

Sending Custom Header/Response from Controller:

In magento2, it is possible, from a controller execute method, to manipulate the request to send a custom header and error page.

First of all, to comply with action controller interface \Magento\Framework\App\ActionInterface::execute(), your action must return an instance of \Magento\Framework\Controller\ResultInterface (\Magento\Framework\App\ResponseInterface is also supported, but is legacy and will be removed in future releases of M2, when all core usages are refactored).

So choose from available implementations of \Magento\Framework\Controller\ResultInterface. The most suitable for custom REST API (assuming it operates with JSON) seems to be \Magento\Framework\Controller\Result\Json. However, if you need something even more custom, consider \Magento\Framework\Controller\Result\Raw.

Here, we are first creating instance of ResultFactory by calling create()and then by using it we are redirecting to another URL using setURL() method. In the setUrl() method, we pass the desired URL. Here we wand to redirect the action to previous page, so we are passing $this->_redirect->getRefererUrl() as parameter.

Now, lets move to another type of redirection, in which we need to redirect the custom action to any desired URL. There are several methods to accomplish this task. Here, I’m going to show two simple methods

1. This method is somehow similar to the method explained above. There is a minor change as follows:

If we use [‘_current’ => true] then the objects being passed via query string will remain in the redirected URL but if we don’t use [‘_current’ => true] or use [‘_current’ => false], then objects will not be passed in the redirected URL.

URL Builder:

Suppose we want to redirect from a page to another page using anchor tag or similar, then we would need to set the href attribute to the URL to which it needs to redirect the current page. Here we should not use hard code like http://127.0.0.1/magento2/index.php/excellence/index/add/. Magento 2 comes with the concept of URL builder. it builds the actual URL of the controller action which is to be used. We have to just use:

$this->_urlBuilder->getUrl("excellence/index/add/")

If we print it then we will get the URL: http://127.0.0.1/magento2/index.php/excellence/index/add/. This method is somehow similar to classic method Mage::getUrl() of magento 1.

Share this:

Like this:

Magento registry stores data to memory which is specific to that request (rather than user or anything else), and persists for the duration of that request only. The principle is very simple really, the Mage class is instantiated as a singleton object for every request and the instantiated Mage object remains in memory, and is accessible in all classes (and templates) until the request completes and the response is sent.

As the object is a singleton, whenever you access it you get the same object. All that is happening is that you are storing values to this object, so when one class stores a value, and another accesses it they are both working on the same object and the second class is able to retrieve the value the first class set.

In magento 1, we were using Mage::register() and Mage::registry(). But in Magento2, the implementation is slightly different. Here we use class Magento\Framework\Registry, and we can inject it in any constructor as follows: