The article is dedicated to flushing data in Magento 2. Learning one of the significant processes in Magento, you’ll be able to make a distinction between Magento 1 and 2 and go up for the Magento exam.
Flushing data in Magento 2 (read here about Output Flushing in Magento 1) is triggered in \Magento\Framework\App\Bootstrap::run method by calling \Magento\Framework\App\ResponseInterface::sendResponse method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public function run(AppInterface $application) { <strong>try</strong> { <strong>try</strong> { \Magento\Framework\Profiler::start('magento'); $this->initErrorHandler(); $this->initObjectManager(); $this->assertMaintenance(); $this->assertInstalled(); $response = $application->launch(); $response->sendResponse(); \Magento\Framework\Profiler::stop('magento'); } <strong>catch</strong> (\Exception $e) { \Magento\Framework\Profiler::stop('magento'); if (!$application->catchException($this, $e)) { throw $e; } } } catch (\Exception $e) { $this->terminate($e); } } |
Depending on request nature (e.g. http, webapi, console command, etc.), this will call an appropriate method of implementing class. In case of HTTP response it will be \Magento\Framework\HTTP\PhpEnvironment\Response::sendResponse method where method will be called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function send() { $this->sendHeaders() ->sendContent(); return $this; } public function sendContent() { if ($this->contentSent()) { return $this; } echo $this->getContent(); $this->contentSent = true; return $this; } |
Associated events
controller_front_send_response_before – invokes at the and of \Magento\Framework\App\Http::launch just before returning processed response to \Magento\Framework\App\Bootstrap::run method.
Redirects
Internal redirects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
protected $resultRedirectFactory; public function __construct( \Magento\Framework\Controller\ResultFactory $result ) { $this->resultRedirectFactory = $result; } public function execute() { $resultForward = $this->resultRedirectFactory->create(ResultFactory::TYPE_FORWARD); $resultForward>setUrl($this->_redirect->getRefererUrl()); return $resultForward; } |
HTTP redirects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
protected $resultRedirect; public function __construct( \Magento\Framework\Controller\ResultFactory $result ) { $this->resultRedirect = $result; } public function execute() { $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($this->_redirect->getRefererUrl()); return $resultRedirect; } |
You are welcome, Syed!
Comprehensive and useful guide. Thanks for providing this awesome post.