A couple of years ago I wrote about Exporting Data to a Downloadable CSV File with CakePHP which is still getting a lot of interest; however, at the time it was pointed out that a better way of handling CSVs in Cake was using a View Class.

Now that CakePHP 3 is out I thought it would be a good idea to write an updated piece on exporting data to a CSV using the excellent CsvView plugin.

Using CsvView significantly simplifies generating the CSV, removing the chore of having to manually iterate over data, especially if needed for more than one export method. So the fist step is to install the plugin:-

$ composer require friendsofcake/cakephp-csvview:~3.0

Next we need to load the plugin in config/bootstrap.php:-


As an example we’ll consider a Subscriber model that contains contact details of people who have subscribed to our app. We want to create a CSV export of all the subscribers’ data.

The Controller

Let’s start with the controller. We need to create an action in the Subscriber’s Controller for generating the CSV export:-

// src/Controller/SubscribersController.php
class SubscribersController extends AppController {
	public function export() {
		$data = $this->Subscriber->find('all')->toArray();
		$_serialize = 'data';
   		$this->set(compact('data', '_serialize'));

Let’s run through what this code is doing. Firstly we’re setting the relevant headers so that the browser will know to download the file:-


When you trigger the controller’s action from the browser it will start to download a file named export.csv rather than attempt to display the view in the browser.

Next we grab the data from the database and set it ready for the View (the $data array):-

$data = $this->Subscriber->find('all')->toArray();
$_serialize = 'data';
$this->set(compact('data', '_serialize'));

It’s important to also set the $_serialize variable so that the View class will know which variables to use for rendering the CSV; this is the same as how Cake’s JsonView and XmlView work if you’re familiar with them.

Finally we tell Cake to use the Csv View class provided by the plugin:-


This will handle the rendering of our data without the need for creating a View template and having to handle the content ourselves.

Setting a Header Row

So far our CSV file is a simple output of all our subscribers’ data, it would be useful to include a header row at the top of our file. With CsvView we can do that by setting $_header:-

$_header = ['ID', 'Name', 'Email'];
$this->set(compact('data', '_serialize', '_header'));

If you need a footer row you can do much the same as the header using $_footer.

Restricting Columns Included in the CSV

There will be times where we also only want to include specific columns from our find() in our CSV file. We can do this using $_extract like this:-

$_extract = ['id', 'name', 'email_address'];
$this->set(compact('data', '_serialize', '_header', '_extract'));

The Csv View class is very powerful and you can do a lot more with it than shown here. Check out the plugin’s documentation for full details.

The Route

As it currently stands the URL for our CSV export will be ‘subscribers/export’; it would be nice to append that with the CSV extension. To do that we’re going to tell Cake to correctly parse CSV file requests. In our config/routes.php file add the following:-


This will now allow us to access our CSV export from the URL ‘subscribers/export.csv’.


Everything should now be setup, we just want to be able to link to our CSV export from a View. If you’re parsing the file extension then you need to include this in the URL; so using the HtmlHelper we need to include the ‘_ext’ parameter for the file extension:-

<?= $this->Html->link('export', [
	'controller' => 'subscribers', 
	'action' => 'export',
	'_ext' => 'csv'
]) ?>

That should be it! Clicking on the ‘export’ link should start the browser downloading our export.csv file.


Can I export an image into a csv file using this plugin?

Shain – 11 Mar 2016 6:41am

Hi Shain, CSVs are just plain text files so you can’t exactly embed an image into the file.

Andy – 11 Mar 2016 2:52pm

After composing i am getting this error…

Installation failed, reverting ./composer.json to its original content.

[ErrorException] Undefined variable: baseDir

Can you please help me.?
Thank You in advance,

Sivaram – 16 Mar 2016 11:26am

hi and thank you for your post !

unfortunely, I have this error :

The action export.csv is not defined in ConferencesController
Error: Create ConferencesController::export.csv() in file: src/Controller/ConferencesController.php.

Can you help me ?

Sid – 5 Aug 2016 11:48am

Hi Andy,

Thanks for your job.
How do I load another view simultaneously export ?

Sébastien – 19 Aug 2016 7:24am

Thanks for the post, help me a lot.

Tiago – 8 Feb 2017 1:16pm

How i set the format of the 1 column with this plugin?

James – 17 Feb 2017 7:33pm

Thanks for the tutorial super intelligible worked perfect

Rosa Rojas – 11 Mar 2017 5:46pm

thank you so much

PhamSinh – 1 Jun 2017 4:35am

Good morning, I worked perfect plugin that good contribute many thanks, but you can help me I do not know how to put the field of another controller? In my view I use several tables. I appreciate your collaboration

jose fernando – 3 Jul 2017 7:46pm

Is this plugin capable to make XLS file? Within csv exported, Excel 2016 shows all fields in just one column.

Christian – 10 Jul 2017 3:02pm

Comments are now closed.