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 .
public function __construct( \Magento\Framework\App\Action\Context $context ) { $this->_request = $context->getRequest(); $this->_response = $context->getResponse(); $this->resultRedirectFactory = $context->getResultRedirectFactory(); $this->resultFactory = $context->getResultFactory(); }
and give you two getters for using in the children class :
/** * Retrieve request object * * @return \Magento\Framework\App\RequestInterface */ public function getRequest() { return $this->_request; } /** * Retrieve response object * * @return \Magento\Framework\App\ResponseInterface */ public function getResponse() { return $this->_response; }
Ok inside your action you can get it :
$request = $this->getRequest() ;
the request object give you many method you can find inside interface RequestInterface :
public function getModuleName(); public function setModuleName($name); public function getActionName(); public function setActionName($name); public function getParam($key, $defaultValue = null); public function getParams(); public function getCookie($name, $default); public function isSecure();
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 :
if($request->getModuleName() == “Magento_Catalog”) { //you code }
B – get value of parameter in url :
$id = $request-> getParam(“id”,0);
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):
$this->getRequest()->setModuleName($currentModuleName); $this->getRequest()->setControllerName($currentControllerName); $this->getRequest()->setActionName($currentActionName);
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:
return $this->getResponse()->representJson($this->jsonHelper->jsonEncode($response_array));
the $this->jsonHelper is instance of \Magento\Framework\Json\Helper\Data you can injected by DI
B – Append content to body :
$this->getResponse()->appendBody($this->jsonHelper->jsonEncode($data));
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 :
$resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('customer/account/'); return $resultRedirect;
Comments