Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 Export To CSV

Asyraf Wahi Anuar Updated 3 min read

This tutorial will show how to export a list of records to .csv file format in CakePHP 4. This function is useful if you want to extract the record into .csv format which can be open using Microsoft Excel. The CakePHP-csv view plugin will be used to generate the .csv file.

This tutorial may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

This tutorial will show how to export a list of records to .csv file format in CakePHP 4. This function is useful if you want to extract the record into .csv format which can be open using Microsoft Excel. The CakePHP-csvview plugin will be used to generate the .csv file.

Download the plugin from the Github repository using composer:

Terminal
composer require friendsofcake/cakephp-csvview

Load the plugin using the console:

Terminal
bin/cake plugin load CsvView

or manually load the plugin using the following code in ...\src\Application.php:

src/Application.phpPHP
public function bootstrap(): void
{
    $this->addPlugin('CsvView');

Let assume that the list to be exported is a user table. You need to create a new public function to process the request as shown below (it will export all columns and download with file name user.csv):

src/Controller/UsersController.phpPHP
public function csv()
{
    $this->response = $this->response->withDownload('user.csv');
    $users = $this->Users->find();
    $_serialize = 'users';

    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('users', '_serialize'));
}

Or if you want to add a header to your .csv file, add $_header and $_extract as shown below:

PHP
public function csv()
{
    $this->response = $this->response->withDownload('user.csv');
    $users = $this->Users->find();
    $_serialize = 'users';
    $_header = ['ID', 'Name', 'Username', 'Role', 'Created', 'Modified'];
    $_extract = ['id', 'name', 'username', 'role', 'created', 'modified'];

    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('users', '_serialize', '_header', '_extract'));
}

In your index file, create a link/button to download the .csv file:

PHP
<?= $this->Html->link('Download CSV', ['action' => 'csv']) ?>

That all. Happy coding :)

Did this tutorial help?

235 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

Using Bahasa Melayu Table Names in CakePHP

Learn how to use Bahasa Melayu table names like "pengguna" in CakePHP while following the framework’s naming conventions. Includes tips for using the Bake tool and avoiding pluralization issues.

5 min read

Tools & Software

Formatting PHP in VSCode Using Prettier and PHP Intelephense

Learn how to set up Visual Studio Code (VSCode) for automatic PHP file formatting using the Prettier and PHP Intelephense extensions. This step-by-step tutorial will help you maintain a clean and organised codebase, enhancing your PHP development experience.

2 min read