1

In one of my get methods is it possible to specify a specific Content-Type and Content-Disposition? After getting my data back I'm doing this:

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

and so the data returned is in a CSV format. Now I want to set the content type to text/csv and set a Content-Disposition so that it actually allows the file download, instead of just showing the content.

Arul Kumaran
  • 983
  • 7
  • 23
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
  • May I ask what is the exact purpose? I may have a better solution! – Arul Kumaran Apr 12 '15 at 14:21
  • The server has a database behind it with data for the month, and the client wants a button on the webpage that downloads the data as an XLS file. My thought was that the get method would call the stored procedure to get the data, convert it to a CSV, and then send that back as a file so that when they hit the button they were given the "Save As" dialog for the "excel" file. – Gargoyle Apr 12 '15 at 18:17

1 Answers1

1

Here is a working example!

In 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;
    }
}

Your index.php

<?php

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

use Luracast\Restler\Restler;

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

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

Try http://localhost/excel/download

Arul Kumaran
  • 983
  • 7
  • 23