Magento 2 now introduces the Framework result object for handing requests that will handling non page results such as JSON, redirects and other non html returns.
Generally, in the Magento2 controller’s execute() function. Sometime we need to return the JSON format data or even the raw text data. The Controller in Magento2 can return several response type depending the purose of result.
Responses Types :
- Page : This returns HTML loaded from a layout handle.
Class : \Magento\Framework\View\Result\Page
a. Create Controller File.
File Path : app/code/V4U/ControllerResponseTypes/Controller/Index/Resultpage.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace V4U\ControllerResponseTypes\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\RequestInterface; use Magento\Framework\View\Result\PageFactory; class Resultpage extends Action { /** * The PageFactory to render with. * * @var PageFactory */ protected $_resultsPageFactory; /** * Set the Context and Result Page Factory from DI. * @param Context $context * @param PageFactory $resultPageFactory */ public function __construct( Context $context, PageFactory $resultPageFactory ) { $this->_resultsPageFactory = $resultPageFactory; parent::__construct($context); } /** * Show the Controller Response Types Page Result. * * @return \Magento\Framework\View\Result\Page */ public function execute() { return $this->_resultsPageFactory->create(); } } |
b. Create Block file.
File Path : app/code/V4U/ControllerResponseTypes/Block/ControllerResponseTypes.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace V4U\ControllerResponseTypes\Block; class ControllerResponseTypes extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Backend\Block\Template\Context $context, array $data = [] ) { parent::__construct($context, $data); } public function getHelloWorld() { return 'Hello World'; } public function getPageResonse() { return "Controller Response Page Result"; } } |
c. Create Layout file.
File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultpage.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <title> V4U Controller Response Types (Page Result) </title> </head> <body> <referenceContainer name="content"> <block class="V4U\ControllerResponseTypes\Block\ControllerResponseTypes" name="pageresult_response" template="V4U_ControllerResponseTypes::result_page.phtml" /> </referenceContainer> </body> </page> |
d. Create PHTML file.
File Path : app/code/V4U/ControllerResponseTypes/view/frontend/templates/result_page.phtml
1 2 3 |
<h2> <?php echo $block->getPageResonse(); ?> </h2> |
Run the magento commands and launch the below url.
Url : www.example.com/controllerresponsetypes/index/resultpage
2. JSON : this returns a response in JSON format. It can be used in API or AJAX requests.
Class : \Magento\Framework\Controller\Result\Json
a. Create Controller file.
File Path : app/code/V4U/ControllerResponseTypes/Controller/Index/Resultjson.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace V4U\ControllerResponseTypes\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\RequestInterface; use Magento\Framework\View\Result\PageFactory; class Resultjson extends Action { /** * The PageFactory to render with. * * @var PageFactory */ protected $_resultsPageFactory; /** * Set the Context and Result Page Factory from DI. * @param Context $context * @param PageFactory $resultPageFactory */ public function __construct( Context $context, PageFactory $resultPageFactory ) { $this->_resultsPageFactory = $resultPageFactory; parent::__construct($context); } /** * Show the Controller Response Types JSON Result. * * @return \Magento\Framework\View\Result\Page */ public function execute() { return $this->_resultsPageFactory->create(); } } |
Create new Controller file for JSON response.
File Path : app/code/V4U/ControllerResponseTypes/Controller/Index/Responsejson.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php namespace V4U\ControllerResponseTypes\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; class Responsejson extends Action { /** * The JsonResultFactory to render with. * * @var jsonResultFactory */ protected $jsonResultFactory; /** * Set the Context and Result Page Factory from DI. * @param Context $context * @param JsonResultFactory $jsonResultFactory */ public function __construct( Context $context, \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory ) { $this->jsonResultFactory = $jsonResultFactory; parent::__construct($context); } /** * Show the Controller Response Types JSON Result. * * @return \Magento\Framework\Controller\Result\Json */ public function execute() { $result = $this->jsonResultFactory->create(); $data = [ 'foo' => 'bar', 'success' => true ]; $result->setData($data); return $result; } } |
b. Create Layout file.
File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultjson.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <title> V4U Controller Response Types (JSON Result) </title> </head> <body> <referenceContainer name="content"> <block class="V4U\ControllerResponseTypes\Block\ControllerResponseTypes" name="jsonresult_response_vk" template="V4U_ControllerResponseTypes::result_json.phtml" /> </referenceContainer> </body> </page> |
Create Layout file for JSON response.
File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_responsejson.xml
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <title> V4U Controller Response Types </title> </head> <body> </body> </page> |
c. Create PHTML file
File Path : app/code/V4U/ControllerResponseTypes/view/frontend/templates/result_json.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<button class="result_json">Get JSON Result</button> <script type="text/javascript"> require(['jquery', 'jquery/ui'], function($){ $(document).on('click','.result_json',function (){ $.ajax({ context: '#ajaxresponse1', url: '<?= $this->getBaseUrl();?>controllerresponsetypes/index/responsejson', type: "GET", }).done(function (response) { alert(response.success); }); }); }); </script> |
Run the magento commands and launch the below url.
Url : www.example.com/controllerresponsetypes/index/resultjson
3. Raw : this returns whatever you want to be returned.
Class : \Magento\Framework\Controller\Result\Raw
a. Create Controller file.
File Path : app/code/V4U/ControllerResponseTypes/Controller/Index/Resultraw.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace V4U\ControllerResponseTypes\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Controller\Result\RawFactory; class Resultraw extends Action { /** * Set the Context and Result Page Factory from DI. * @param Context $context * @param RawResultFactory $rawResultFactory */ public function __construct( Context $context, RawFactory $rawResultFactory ) { $this->rawResultFactory = $rawResultFactory; parent::__construct($context); } /** * Show the Controller Response Types Raw Result. * * @return \Magento\Framework\Controller\Result\Raw */ public function execute() { $result = $this->rawResultFactory->create(); $result->setHeader('Content-Type', 'text/xml'); $result->setContents("<note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>"); return $result; } } |
b. Create Layout file.
File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultraw.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <title> V4U Controller Response Types (Raw Result) </title> </head> </page> |
Run the magento commands and launch the below url.
Url : www.example.com/controllerresponsetypes/index/resultraw
4. Forward : this internally forwards to another controller without changing the URL.
Class : \Magento\Framework\Controller\Result\Forward
a. Create Controller file.
File Path : app/code/V4U/ControllerResponseTypes/Controller/Index/Resultforward.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php namespace V4U\ControllerResponseTypes\Controller\Index; use Magento\Framework\App\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\Result\Forward; use Magento\Framework\Controller\Result\ForwardFactory; use Magento\Framework\Controller\ResultInterface; /** * Class Resultforward * * @package V4U\ControllerResponseTypes\Controller\Index */ class Resultforward extends Action\Action { /** * @var ForwardFactory */ protected $_resultForwardFactory; /** * Page4 constructor. * @param Context $context * @param ForwardFactory $_resultForwardFactory */ public function __construct( Context $context, ForwardFactory $_resultForwardFactory ) { $this->_resultForwardFactory = $_resultForwardFactory; parent::__construct($context); } /** * Forward home page * * @return \Magento\Framework\Controller\Result\Forward */ public function execute() { $resultForward = $this->_resultForwardFactory->create(); $resultForward->setController('index') ->setModule('cms') ->forward('index'); return $resultForward; } } |
b. Create Layout file.
File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultforward.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <title> V4U Controller Response Types (Forward Result) </title> </head> </page> |
Run the magento commands and launch the below url.
Url : www.example.com/controllerresponsetypes/index/resultforward
5. Redirect : this represents a 301 or 302 redirect. It is used when a user needs to be redirected to a different URL.
Class : \Magento\Framework\Controller\Result\Redirect
a. Create Controller file.
File Path : app/code/V4U/ControllerResponseTypes/Controller/Index/Resultredirect.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php namespace V4U\ControllerResponseTypes\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\RequestInterface; use Magento\Framework\View\Result\PageFactory; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\Controller\Result\Redirect; /** * Class Resultredirect * * @package V4U\ControllerResponseTypes\Controller\Index */ class Resultredirect extends Action { /** * @var \Magento\Framework\Controller\Result\RedirectFactory */ protected $resultRedirectFactory; public $_storeManager; public function __construct( Context $context, StoreManagerInterface $storeManager, Redirect $resultRedirectFactory ) { $this->resultRedirectFactory = $resultRedirectFactory; $this->_storeManager=$storeManager; parent::__construct($context); } /** * Show the Controller Response Types Redirect Result. * * @return (\Magento\Framework\Controller\Result\Redirect */ public function execute() { $resultRedirect = $this->resultRedirectFactory->create(); $redirectLink = $this->_storeManager->getStore()->getBaseUrl().'customer/account/login'; $resultRedirect->setUrl($redirectLink); return $resultRedirect; } } |
b. Create Layout file.
File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultredirect.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <title> V4U Controller Response Types (Redirect Result) </title> </head> </page> |
Run the magento commands and launch the below url.
Url : www.example.com/controllerresponsetypes/index/resultredirect
You can download module from GitHub here.
Happy Coding. Keep Liking & Sharing