It's actually really easy, you'll need to create a view that will just loop the content, and write it out, and change the header so that it will download a file with a csv file.
Here's the code
-
// Let's say example is id, name, surname, gender
-
// Controller - ExamplesController.php
-
class ExamplesController extends AppController
-
{
-
/**
-
* csv method
-
*
-
* @return void
-
*/
-
public function csv()
-
{
-
$this->layout = 'csv';
-
$this->set('examples', $this->Example->find("all"));
-
}
-
}
-
// Layout - csv.ctp
-
//generate csv header
-
header("Content-type: application/octet-stream");
-
header("Content-Disposition: attachment; filename=export.csv");
-
header("Pragma: no-cache");
-
header("Expires: 0");
-
echo $this->fetch('content');
-
// View - csv.ctp
-
foreach ($examples as $example) {
-
echo implode(",",$example["Example"]);
-
}
// Let's say example is id, name, surname, gender
// Controller - ExamplesController.php
class ExamplesController extends AppController
{
/**
* csv method
*
* @return void
*/
public function csv()
{
$this->layout = 'csv';
$this->set('examples', $this->Example->find("all"));
}
}
// Layout - csv.ctp
//generate csv header
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $this->fetch('content');
// View - csv.ctp
foreach ($examples as $example) {
echo implode(",",$example["Example"]);
}