本文整理汇总了PHP中Magento\Framework\App\ResponseInterface::clearBody方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::clearBody方法的具体用法?PHP ResponseInterface::clearBody怎么用?PHP ResponseInterface::clearBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Framework\App\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::clearBody方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* Declare headers and content file in response for file download
*
* @param string $fileName
* @param string|array $content set to null to avoid starting output, $contentLength should be set explicitly in
* that case
* @param string $baseDir
* @param string $contentType
* @param int $contentLength explicit content length, if strlen($content) isn't applicable
* @throws \Exception
* @throws \InvalidArgumentException
* @return \Magento\Framework\App\ResponseInterface
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExitExpression)
*/
public function create($fileName, $content, $baseDir = DirectoryList::ROOT, $contentType = 'application/octet-stream', $contentLength = null)
{
$dir = $this->_filesystem->getDirectoryWrite($baseDir);
$isFile = false;
$file = null;
if (is_array($content)) {
if (!isset($content['type']) || !isset($content['value'])) {
throw new \InvalidArgumentException("Invalid arguments. Keys 'type' and 'value' are required.");
}
if ($content['type'] == 'filename') {
$isFile = true;
$file = $content['value'];
if (!$dir->isFile($file)) {
throw new \Exception((string) new \Magento\Framework\Phrase('File not found'));
}
$contentLength = $dir->stat($file)['size'];
}
}
$this->_response->setHttpResponseCode(200)->setHeader('Pragma', 'public', true)->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)->setHeader('Content-type', $contentType, true)->setHeader('Content-Length', $contentLength === null ? strlen($content) : $contentLength, true)->setHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"', true)->setHeader('Last-Modified', date('r'), true);
if ($content !== null) {
if ($isFile) {
$this->_response->sendHeaders();
$stream = $dir->openFile($file, 'r');
while (!$stream->eof()) {
echo $stream->read(1024);
}
$stream->close();
flush();
if (!empty($content['rm'])) {
$dir->delete($file);
}
$this->callExit();
} else {
$this->_response->clearBody();
$this->_response->setBody($content);
}
}
return $this->_response;
}