Flushing Data (Output) (Magento Certified Developer Exam)

January 29, 2013 Andrew Matskevich Certification
Flushing Data (Output) (Magento Certified Developer Exam)

Data output to the browser

Data for output to the browser accumulates in the controller in the internal variable Mage_Core_Controller_Response_Http::_body, and is displayed by calling Mage_Core_Controller_Response_Http::appendBody(). In most cases, processing is carried out through the Layout object and is ended by calling $this->renderLayout():

/app/code/core/Mage/Core/Controller/Varien/Action.php

The line $this->getLayout()->setDirectOutput(false); includes output buffering.

For calls in which Layout is not used, for example, via Ajax, content is set directly by calling $this->getResponse()->appendBody($output); :

After executing the controller, direct output to the browser is carried out:

/app/code/core/Mage/Core/Controller/Varien/Front.php

Before the output of data to the browser the event controller_front_send_response_before is run, after it – controller_front_send_response_after. The event controller_front_send_response_before may contain additional headers, that is why it is important to use the method $this->getResponse()-> appendBody() instead of the simple echo output in the controller. Otherwise, the headers installed after the controller will not be applied.

Let us take a close look at Zend_Controller_Response_Http::sendResponse public function sendResponse()

First, the function sendHeaders() applies all the standard headers, accumulated through the call $this->getResponse()->setHeader();. Next, the error handling in the content output is performed:

 

Redirect

There are two main methods for redirect in the controller:

  1. _redirect();
  2. _forward();

_redirect method performs an external redirect, i.e. redirects the user to a new URL setting http header Location and passes the session.

_forward method performs an internal redirect in Magento, invisible to the user. _forward method can redirect execution to another module, controller and action:

 

Combining Javascript and CSS files

Output of Javascript and CSS files is carried out by calling Mage_Page_Block_Html_Head::getCssJsHtml().

Array $this->_data[‘items’] contains the list of add-on CSS and JavaScript files and is formed by calling addItem in Layout. In /app/design/frontend/base/default/layout/page.xml the following can be found:

For headers to be displayed, the block Mage_Page_Block_Html_Head is created and addJs, addCss, addItem methods are called. addJs, addCss methods call addItem:

Depending on the settings dev / js / merge_files, dev / css / merge_css_files CSS and JavaScript can be output separately or combined. If the setting dev / js / merge_files (System-> Configuration-> ADVANCED (Developer) Merge JavaScript Files) is enabled, all JavaScript files will be combined into one single file.

_prepareStaticAndSkinElements method determines the correct path and prepares the HTML output by the template passed by the first parameter. Each JavaScript file can get not only a set of  <script>  inserts returned, but also a single one that includes the path to the merged file, for determining of  which Mage_Core_Model_Design_Package::getMergedJsUrl() method is called:

Having determined the name of the future file, let us move to the process of creating this file. Mage_Core_Model_Design_Package::_mergeFiles() method synchronizes files with the database if the corresponding option is enabled, and calls Mage::helper (‘core’) ->mergeFiles().In case the file has already been created this method determines the need to update by comparing the edit time of the source files and the created one. If the update is needed the method filters files by their extensions, loads, merges the contents of the source files and stores the content in the merged file. Similarly for the CSS files, when dev/css/merge_css_files (System->Configuration->ADVANCED (Developer) Merge CSS Files) setting is enabled all CSS files are merged into one shared CSS file.

In this case, Mage_Core_Model_Design_Package::getMergedCssUrl() method is run instead of the method getMergedJsUrl. Besides the names of included files, host and port are also taken into account to form the file name.  

As in the case of JavaScript, the method Mage_Core_Model_Design_Package::mergeFiles is called, into which the callback function Mage_Core_Model_Design_Package::beforeMergeCss() is passed by the third parameter to adjust the file paths in CSS (used in constructions @import and url()).



2 comments

  1. Hi,

    Thanks for your post. Quick question!?

    Why are the host and port taken into account when forming the merged CSS filename?

    Cheers,

    Jay

  2. Hello Jay!
    You forgot about multi-store functionality. Every store has unique theme (skin). But filenames in all skins are the same. Only package or theme name differs. So if you use only filenames as hash you’ll rewrite existing merged file that belongs to another store. So with use of hostname and port you create unique filename for each store. We can compare host and port parts with salt in the password hashing procedure.

Post a new comment

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