/   /   /  Magento 2 Action: the default injected objects ($request $response,$resultRedirectFactory )

Note:

For more extensions and themes visit our store

Magento 2 Action: the default injected objects ($request $response,$resultRedirectFactory )

Magento 2 Action allow the developers using $request $response and $resultRedirectFactory  objects without injecting (DI) by hand .
By extending \Magento\Backend\App\Action for backend action case or \Magento\Framework\App\Action\Action for global case , you have $request $response and $resultRedirectFactory .

1 – Request :

the top level parent class of action is Magento\Framework\App\Action\AbstractAction ,  AbstractAction injected by di with \Magento\Framework\App\Action\Context $context .
 

  1.  
  2.     public function __construct(
  3.         \Magento\Framework\App\Action\Context $context
  4.     ) {
  5.         $this->_request = $context->getRequest();
  6.         $this->_response = $context->getResponse();
  7.         $this->resultRedirectFactory = $context->getResultRedirectFactory();
  8.         $this->resultFactory = $context->getResultFactory();
  9.     }
  10.  
  11.  

and give you two getters for using in the children class :
 
  1.  
  2.     /**
  3.      * Retrieve request object
  4.      *
  5.      * @return \Magento\Framework\App\RequestInterface
  6.      */
  7.     public function getRequest()
  8.     {
  9.         return $this->_request;
  10.     }
  11.  
  12.     /**
  13.      * Retrieve response object
  14.      *
  15.      * @return \Magento\Framework\App\ResponseInterface
  16.      */
  17.     public function getResponse()
  18.     {
  19.         return $this->_response;
  20.     }
  21.  
  22.  

Ok inside your action you can get it :
$request = $this->getRequest() ;
the request object give you many method you can find inside interface RequestInterface  :
 

  1.  
  2.     public function getModuleName();
  3.     public function setModuleName($name);
  4.     public function getActionName();
  5.     public function setActionName($name);
  6.     public function getParam($key, $defaultValue = null);
  7.     public function setParams(array $params);
  8.     public function getParams();
  9.     public function getCookie($name, $default);
  10.     public function isSecure();
  11.  
  12.  

Example of how use:

A - ok imagine we want restrict the access to categories by customer group and we want execute a code just if we point in the specific module :
 

  1.  
  2.      if($request->getModuleName()  == “Magento_Catalog”)
  3.     {
  4.       //you code
  5.     }
  6.  
  7.  

B – get value of parameter in url :
 

  1.  
  2.      $id = $request-> getParam(“id”,0);
  3.  
  4.  

the secode attribute is the default value if the parameter doesn't exist in url

C – forward to other module controller and action (this method isusefulin unit test):
 

  1.  
  2.      $this->getRequest()->setModuleName($currentModuleName);
  3.      $this->getRequest()->setControllerName($currentControllerName);
  4.      $this->getRequest()->setActionName($currentActionName);
  5.  
  6.  

But for almost for full stack method you need open Magento\Framework\App\Request\Http because Magento 2 when you  call RequestInterface  give you with preference  Http class as result .

 

2 – Response :

Ok see :
    <preference for="Magento\Framework\App\ResponseInterface" type="Magento\Framework\App\Response\Http" />

A – Return Json Content:
 

  1.  
  2. return $this->getResponse()->representJson($this->jsonHelper->jsonEncode($response_array));
  3.  
  4.  

the $this->jsonHelper is instance of \Magento\Framework\Json\Helper\Data you can injected by DI


B – Append content to body :
 

  1.  
  2. $this->getResponse()->appendBody($this->jsonHelper->jsonEncode($data));
  3.  
  4.  

C – Public and private cache :
you can use two method for that :
setPrivateHeaders($ttl) and setPublicHeaders($ttl) ;
$ttl :is int variable represent max-age and expire date by second
To handle this situation, every response may be set to be public or private:
public
Indicates that the response may be cached by both private and shared caches.
private
Indicates that all or part of the response message is intended for a single user and must not be cached by a shared cache.

 

3 – resultRedirectFactory :

Yes for redirect your page to other Url you can use :
 

  1.  
  2. $resultRedirect = $this->resultRedirectFactory->create();
  3. $resultRedirect->setPath('customer/account/');
  4. return $resultRedirect;
  5.  
  6.  

 

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.

Newsletter