Magento2

Magento 2 Controller Response Types – Page, JSON, ROW, Redirect, Forward

Magento 2 Controller Response Types - Page, JSON, ROW, Redirect, Forward

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 :

  1. 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

b. Create Block file.

File Path : app/code/V4U/ControllerResponseTypes/Block/ControllerResponseTypes.php

c. Create Layout file.

File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultpage.xml

d. Create PHTML file.

File Path : app/code/V4U/ControllerResponseTypes/view/frontend/templates/result_page.phtml

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

Create new Controller file for JSON response.

File Path : app/code/V4U/ControllerResponseTypes/Controller/Index/Responsejson.php

b. Create Layout file.

File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultjson.xml

Create Layout file for JSON response.

File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_responsejson.xml

c. Create PHTML file

File Path : app/code/V4U/ControllerResponseTypes/view/frontend/templates/result_json.phtml

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

b. Create Layout file.

File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultraw.xml

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

b. Create Layout file.

File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultforward.xml

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

b. Create Layout file.

File Path : app/code/V4U/ControllerResponseTypes/view/frontend/layout/controllerresponsetypes_index_resultredirect.xml

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 💻

Tagged ,