有 Restler 方法 return 一个 Content-Disposition 头

have Restler method return a Content-Disposition header

在我的一种获取方法中是否可以指定特定的 Content-Type 和 Content-Disposition?取回我的数据后,我正在这样做:

return Scope::get('CsvFormat')->encode($data);

因此返回的数据为 CSV 格式。现在我想将内容类型设置为 text/csv 并设置 Content-Disposition 以便它实际上允许文件下载,而不仅仅是显示内容。

这是一个工作示例!

Excel.php

<?php

class Excel
{
    /**
     * Download CSV data which can be rendered with Excel
     *
     * @return array
     * @format CsvFormat
     * @header Content-Disposition: attachment; filename="excel.csv"
     */
    public function download()
    {
        //csv compatible array
        $data = [['name' => 'John Doe', 'age' => '23'], ['name' => 'Mika', 'age' => '45']];

        return $data;
    }
}

你的index.php

<?php

require '../vendor/autoload.php';

use Luracast\Restler\Restler;

$r = new Restler();
$r->addAPIClass('Excel');

$r->setOverridingFormats('CsvFormat');
$r->handle();

尝试http://localhost/excel/download