Flushing Data (Output) in Magento 2

May 30, 2017

1271 Aleksander Kutseika

Flushing Data (Output) in Magento 2

The physical process of rendering is conceptually the same as in Magento 1, but it differs from the code and architectural levels. Data meant for output to the browser accumulates in the controller in the internal variable Zend\Stdlib\Message::content, and is displayed by calling Magento\Framework\HTTP\PhpEnvironment::appendBody().

In general terms, the following steps are taken during the rendering and flushing output process:

  1. Custom controller returns an object of the required type.
  2. The front controller (Magento\Framework\App-\FrontController) dispatches a request and gets a result object.
  3. The app (Magento\Framework\App\Http) in the launch() method copies HTML to the response object.
  4. The bootstrap (Magento\Framework\App\Bootstrap) flushes that HTML from the response objects to the browser.

Flushing Data (Output) in Magento 2Flushing Data (Output) in Magento 2

1.1 PageObject can be injected into the controller, created, and returned from the execute() method:


1.2 JSON result object, created in the execute() method, can be injected into a controller.

Usually a controller returns an object as the result of the call $result->setData($data), which is a $result object. The setData() method is used to assign the array to return as JSON.


magento development services

Magento 2 Development

Take your online store to the next level with BelVG Magento 2 Development

Visit the page

2. Result objects are got back to the FrontController


3. And then go to the App object (Magento\Framework\App\Http), to the launch() method:


The event controller_front_send_response_before may contain additional headers or params.

Andrey_Dubina

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

$result->renderResult($this->_response) – the exact place where rendering output occurs.

4. Magento will generate HTML and append it to the response object. Response objects are then got back to the Bootstrap class (run() method) before being flushed:


Magento\Framework\HTTP\PhpEnvironment\Response::sendResponse()


Let`s take a close look at Zend\Http\PhpEnvironment::sent() public function:


First, the function sendHeaders() applies all the standard headers accumulated through the call Magento\Framework\HTTP\PhpEnvironment\Response::setHeader() and sendContent() – display content body:


Redirect

Magento 2 offers more structure around the forward function and redirects than Magento 1.

There are two types of redirect:

  • redirect

Example of redirect result object usage. A redirect header is sent back to the browser, which  redirects the browser to another URL:

Andrey_Dubina

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


Redirect method – Magento\Framework\App\Action\Action::_redirect():

  • forward

An example of forward result object usage. An internal redirect invisible to a customer is performed:


Redirect method – Magento\Framework\App\Action\Action::_forward():


It is important to have both forward and redirect actions, and forward and redirect methods. In fact, they are not the same. Why are both actions and methods applied for these functions? Because of the way the routing loop is performed. The controller returns an action instance and requires a true argument, while the action sets it to false within the loop.

Magento Audit

Magento Audit

Take your online store to the next level with BelVG Magento Audit

Visit the page

Combining Javascript and CSS files

In step 3, when we render output in $result->renderResult($this->_response) method:


if $result instance of Magento\Framework\View\Result\Page class:


There magento2 adds to header content of block with requirejs scripts and $this->pageConfigRenderer->renderHeadContent() add css and js from layout Magento\Framework\View\Page\Config\Renderer::renderHeadContent():


Magento\Framework\View\Page\Config\Renderer::renderAssets() – return renderer HTML for all Assets:


$this->processMerge($group->getAll(), $group) – call to the assets collection (\Magento\Framework\View\Assets\GropedCollection object) and return array of assets sorted by groups. Depending on the Magento settings CSS and Javascript can be returned separately or combined (STORES->Configuration->Developer->JavaScript Settings->Merge JavaScript Files, STORES->Configuration->Developer->CSS Settings->Merge CSS Files). If the settings are enabled, all JavaScript and CSS files will be combined into one single file.

Finally $this->renderAssetHtml($groupTemplate, $groupAssets) render HTML tag referencing to corresponding URLs:


migration to magento 2

Magento 2 Migration

Take your online store to the next level with BelVG Magento 2 Migration

Visit the page
Andrey Dubina

Partner With Us

Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *