PHP code example of yiisoft / response-download

1. Go to this page and download the library: Download yiisoft/response-download library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yiisoft / response-download example snippets


use Psr\Http\Message\ResponseInterface;
use Yiisoft\ResponseDownload\DownloadResponseFactory;

final class MyController
{
    public function __construct(
        private readonly DownloadResponseFactory $downloadResponseFactory,
    )
    {    
    }

    public function sendMyContentAsFile(): ResponseInterface
    {
        return $this->downloadResponseFactory->sendContentAsFile('Hello!', 'message.txt');
    }
    
    public function sendMyFile(): ResponseInterface
    {
        return $this->downloadResponseFactory->sendFile('message.txt');
    }
    
    public function xSendMyFile(): ResponseInterface
    {
        return $this->downloadResponseFactory->xSendFile('message.txt');
    }
    
    public function sendMyStreamAsFile(): ResponseInterface
    {
        $stream = new MyStream();
        
        return $this->downloadResponseFactory->sendStreamAsFile($stream, 'message.txt');
    }
}