As you may know, flushing data is a significant process in Magento. The function allows returning the HTML code to the browser. It implies that flushing output is to display a content of a request in Magento. Let’s clarify the process in Magento 1 in order to be prepared for the Magento exam.
In Magento 1 content rendering begins when controller calls \Mage_Core_Controller_Varien_Action::renderLayout method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public function renderLayout($output='') { $_profilerKey = self::PROFILER_KEY . '::' . $this->getFullActionName(); if ($this->getFlag('', 'no-renderLayout')) { return; } if (Mage::app()->getFrontController()->getNoRender()) { return; } $this->_renderTitles(); Varien_Profiler::start("$_profilerKey::layout_render"); if (''!==$output) { $this->getLayout()->addOutputBlock($output); } Mage::dispatchEvent('controller_action_layout_render_before'); Mage::dispatchEvent('controller_action_layout_render_before_'.$this->getFullActionName()); #ob_implicit_flush(); $this->getLayout()->setDirectOutput(false); $output = $this->getLayout()->getOutput(); Mage::getSingleton('core/translate_inline')->processResponseBody($output); $this->getResponse()->appendBody($output); Varien_Profiler::stop("$_profilerKey::layout_render"); return $this; } |
This method checks if rendering should continue and then calls \Mage_Core_Model_Layout::getOutput which iterates through all available blocks and collects their output into a string and then appends this string to \Mage_Core_Controller_Response_Http object.
Actual sending data to the browser occurs at the end of Front Controller’s method \Mage_Core_Controller_Varien_Front::dispatch when \Zend_Controller_Response_Abstract::sendResponse is called.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function sendResponse() { $this->sendHeaders(); if ($this->isException() && $this->renderExceptions()) { $exceptions = ''; foreach ($this->getException() as $e) { $exceptions .= $e->__toString() . "\n"; } echo $exceptions; return; } $this->outputBody(); } |
This method renders available headers, checks if there is an exception and should it be rendered and then calls \Zend_Controller_Response_Abstract::outputBody which directly sends content to the browser.
1 2 3 4 5 |
public function outputBody() { $body = implode('', $this->_body); echo $body; } |
Sending content to the browser in another way (e.g echoing from controller or model) may result in incorrect (or not at all) headers being sent.
Associated events
controller_front_send_response_before – occurs right before calling \Zend_Controller_Response_Abstract::sendResponse method. It can be used to launch something before sending output (cookies, etc.)
controller_front_send_response_after – same method but after sending output.
controller_action_layout_render_before and controller_action_layout_render_before_{ACTION_NAME} methods – in \Mage_Core_Controller_Varien_Action::renderLayout method before rendering blocks contents.
Redirects
There are two main redirect types in Magento:
1. Internal redirect to another action or controller. This redirect can be done with \Mage_Core_Controller_Varien_Action::_forward method.
1 |
$this->_forward(‘action_name’, ‘controller_name’); |
2. HTTP redirect. HTTP redirects can be done with setRedirect method of \Zend_Controller_Response_Abstract class from any controller.
1 |
$this->getResponse()->setRedirect($this->getUrl("*/*/*")); |