Skip to content

Commit f2a9e05

Browse files
author
vpaladiychuk
committed
MAGETWO-2204: "HEADERS ALREADY SENT" When Controller Action Outputs Directly
1 parent 2242e66 commit f2a9e05

File tree

1 file changed

+17
-4
lines changed
  • app/code/Magento/Customer/Controller/Adminhtml/Index

1 file changed

+17
-4
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/Viewfile.php

100644100755
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
*/
2121
class Viewfile extends \Magento\Customer\Controller\Adminhtml\Index
2222
{
23+
/**
24+
* @var \Magento\Framework\Controller\Result\RawFactory
25+
*/
26+
protected $resultRawFactory;
27+
2328
/**
2429
* @var \Magento\Framework\Url\DecoderInterface
2530
*/
@@ -51,6 +56,7 @@ class Viewfile extends \Magento\Customer\Controller\Adminhtml\Index
5156
* @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
5257
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
5358
* @param \Magento\Framework\Controller\Result\JSONFactory $resultJsonFactory
59+
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
5460
* @param \Magento\Framework\Url\DecoderInterface $urlDecoder
5561
*
5662
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -81,6 +87,7 @@ public function __construct(
8187
\Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
8288
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
8389
\Magento\Framework\Controller\Result\JSONFactory $resultJsonFactory,
90+
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
8491
\Magento\Framework\Url\DecoderInterface $urlDecoder
8592
) {
8693
parent::__construct(
@@ -110,6 +117,7 @@ public function __construct(
110117
$resultForwardFactory,
111118
$resultJsonFactory
112119
);
120+
$this->resultRawFactory = $resultRawFactory;
113121
$this->urlDecoder = $urlDecoder;
114122
}
115123

@@ -151,6 +159,8 @@ public function execute()
151159
throw new NotFoundException();
152160
}
153161

162+
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
163+
$resultRaw = $this->resultRawFactory->create();
154164
if ($plain) {
155165
$extension = pathinfo($path, PATHINFO_EXTENSION);
156166
switch (strtolower($extension)) {
@@ -180,12 +190,15 @@ public function execute()
180190
->clearBody();
181191
$this->getResponse()->sendHeaders();
182192

183-
echo $directory->readFile($fileName);
193+
$resultRaw->setContents($directory->readFile($fileName));
184194
} else {
185195
$name = pathinfo($path, PATHINFO_BASENAME);
186-
$this->_fileFactory->create($name, ['type' => 'filename', 'value' => $fileName], DirectoryList::MEDIA)
187-
->sendResponse();
196+
$this->_response = $this->_fileFactory->create(
197+
$name,
198+
['type' => 'filename', 'value' => $fileName],
199+
DirectoryList::MEDIA
200+
);
188201
}
189-
exit;
202+
return $resultRaw;
190203
}
191204
}

0 commit comments

Comments
 (0)