简体   繁体   中英

Download CSV in Mezzio Framework (Zend/Laminas)

In Mezzion Framework I have the next Handler:

 <?php

namespace Bgc\Handler;

use App\Service\GenerateReportToCSV;
use Bgc\Queue\BGCQueueManager;
use Laminas\Diactoros\Response\TextResponse;
use League\Csv\Writer;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class DownloadBgcReportHandler implements RequestHandlerInterface
{
    protected $bgcQManager;
    protected $reportToCSV;

    public function __construct(BGCQueueManager $bgcQManager, $reportToCSV)
    {
        $this->bgcQManager = $bgcQManager;
        $this->reportToCSV = $reportToCSV;
    }

    public function handle(ServerRequestInterface $request): TextResponse
    {
        $queryParams = $request->getQueryParams();
        $params = [];

        if (isset($queryParams['startDate'])) {
            $starDate = new \DateTime($queryParams['startDate']);
            $params['startDate'] = $starDate->modify('midnight');
        }

        if (isset($queryParams['startDate'])) {
            $endDate = new \DateTime($queryParams['endDate']);
            $params['endDate'] = $endDate->modify('tomorrow');
        }

        $itemsBGC = $this->bgcQManager->getDataToDownload($params);
        $time = time();
        $fileName = "bgc-report-$time.csv";

        $csv = Writer::createFromFileObject(new \SplFileObject());
        $csv->insertOne($this->reportToCSV->getHeadingsBGC());

        foreach ($itemsBGC as $item) {
            $csv->insertOne($item);
        }

        return new TextResponse($csv->getContent(), 200, [
            'Content-Type' => 'text/csv',
            'Content-Transfer-Encoding' => 'binary',
            'Content-Disposition' => "attachment; filename='$fileName'"
        ]);
    }
}

I have the below error:

Whoops\Exception\ErrorException: Declaration of Bgc\Handler\DownloadBgcReportHandler::handle(Psr\Http\Message\ServerRequestInterface $request): Laminas\Diactoros\Response\TextResponse must be compatible with Psr\Http\Server\RequestHandlerInterface::handle(Psr\Http\Message\ServerRequestInterface $request): Psr\Http\Message\ResponseInterface in file /home/peter/proyectos/revelations-thena-api/src/Bgc/src/Handler/DownloadBgcReportHandler.php on line 20

I don't know, to create a downloable file. The hadbler works fine with Json. I tried to change from ResponseInterface to TextResponse.

How can I download file CSV? Thank you

The error you received is telling you that your method signature is not compliant to interface's method signature.

RequestHandlerInterface:

interface RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): ResponseInterface;
}

As you see, the signature states that an object of type ResponseInterface is returned.

You modified the signature:

class DownloadBgcReportHandler implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): TextResponse;
}

The signature must be the same, but then you can return the TextResponse without problem (since it extends Laminas\\Diactoros\\Response , which implements Psr\\Http\\Message\\ResponseInterface )

Just change that and it will works :)

你已经修改了你的处理方法,所以现在你没有满足 RequestHandlerInterface 的要求

Replace the return value for the handler with ResponseInterface enforced in the interface: RequestHandlerInterface

so i think you are best helped with:

<?php

namespace Bgc\Handler;

use App\Service\GenerateReportToCSV;
use Bgc\Queue\BGCQueueManager;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Stream;
use League\Csv\Writer;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class DownloadBgcReportHandler implements RequestHandlerInterface
{
    protected $bgcQManager;
    protected $reportToCSV;

    public function __construct(BGCQueueManager $bgcQManager, $reportToCSV)
    {
        $this->bgcQManager = $bgcQManager;
        $this->reportToCSV = $reportToCSV;
    }

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $queryParams = $request->getQueryParams();
        $params = [];

        if (isset($queryParams['startDate'])) {
            $starDate = new \DateTime($queryParams['startDate']);
            $params['startDate'] = $starDate->modify('midnight');
        }

        if (isset($queryParams['startDate'])) {
            $endDate = new \DateTime($queryParams['endDate']);
            $params['endDate'] = $endDate->modify('tomorrow');
        }

        $itemsBGC = $this->bgcQManager->getDataToDownload($params);
        $time = time();
        $fileName = "bgc-report-$time.csv";

        // $csv = Writer::createFromFileObject(new \SplFileObject());
        // $csv->insertOne($this->reportToCSV->getHeadingsBGC());
        $csv = Writer::createFromString($this->reportToCSV->getHeadingsBGC());

        foreach ($itemsBGC as $item) {
            $csv->insertOne($item);
        }

        $body = new Stream($csv->getContent());

        return new Response($body, 200, [
            'Cache-Control' => 'must-revalidate',
            'Content-Disposition' => 'attachment; filename=' . $fileName,
            'Content-Length' => strval($body->getSize()),
            'Content-Type' => 'text/csv',
            'Content-Transfer-Encoding' => 'binary',
            'Expires' => '0',
            'Pragma' => 'public',
        ]);
    }
}

PS: i have commented the 2 lines in which an empty new \\SplFileObject() was used, because the required param $filename was empty (and i did not want to make a decision there) and added a line with Writer::createFromString() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM