DataTable Data Export in CakePHP 4 Tutorial

Reading Time: 7 minutes

Working & Download Reports in Web applications is very common when we are developing data related web applications. Exporting data into PDF, Excel, a Word Document, CSV is very common in applications.

Inside this article, we will see the concept of Exporting data into multiple formats like PDF, Excel, Copy to Clipboard, etc report in CakePHP 4 using jQuery DataTable plugin. Installation and working methods for all you can find here in detailed steps.

DataTable is a jquery plugin which helps to create a normal table with advance features like searching, sorting, pagination, etc. In this article we will see DataTable data export in CakePHP 4.

Learn More –

Let’s get started.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

Create Database

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.


Successfully, we have created a database.

Database Connection

Open app_local.php file from /config folder. Search for Datasources. Go to default array of it.

You can add your connection details here to connect with your database. It will be like this –


'Datasources' => [
        'default' => [
            'host' => 'localhost',
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
            //'port' => 'non_standard_port_number',

            'username' => 'root',
            'password' => 'sample@123',

            'database' => 'mydatabase',
             * If not using the default 'public' schema with the PostgreSQL driver
             * set it here.
            //'schema' => 'myapp',

             * You can use a DSN string to set the entire configuration
            'url' => env('DATABASE_URL', null),


You can pass host, username, password and database.

Successfully, you are now connected with the database.

Create Migration

Open project into terminal and run this command to create migration file.

$ bin/cake bake migration CreateEmployees

It will create 20220330143909_CreateEmployees.php file inside /config/Migrations folder. Open migration file and write this following code into it.

The code is all about for the schema of employees table.



use Migrations\AbstractMigration;

class CreateEmployees extends AbstractMigration
    public function change()
        $table = $this->table('employees');
        $table->addColumn("name", "string", [
            "limit" => 50,
            "null" => false
        $table->addColumn("email", "string", [
            "limit" => 50,
            "null" => false
        $table->addColumn("mobile", "string", [
            "limit" => 20,
            "null" => false

Run Migration

Back to terminal and run this command.

$ bin/cake migrations migrate

It will create employees table inside database.

Create Test Data

Here, we have a list of fake data which you needs to be inserted into database table employees. Copy this mysql query and execute it.

Right now, we have only few rows. You can insert as many rows as you want.

-- Dumping data for table `employees`

INSERT INTO `employees` (`id`, `name`, `email`, `mobile`) VALUES
(1, 'Sanjay Kumar', '', '1234567895'),
(2, 'Ashish Kumar', '', '7412589635'),
(3, 'Vijay Kumar', '', '9632587410'),
(4, 'Dhananjay Negi', '', '8529637410'),
(5, 'Ajit Kumar', '', '9658741230'),
(6, 'Suman Yadav', '', '2635897410'),
(7, 'Mandeep Singh', '', '8526937410'),
(8, 'Sourav Sukhla', '', '8974563210'),
(9, 'Raman Singh', '', '8596347158'),
(10, 'Suneel Rana', '', '1547896304'),
(11, 'Vikram Singh', '', '8526934710');

After insertion of test data, table will look like this.

Create Model & Entity


We will create model and entity. Back to terminal and run this command.

$ bin/cake bake model Employees --no-validation --no-rules

It will create model file EmployeesTable.php inside /src/Model/Table folder. Also we should see the entity file Employee.php inside /src/Model/Entity folder.

Create Controller

Open project into terminal and run this command into it.

$ bin/cake bake controller Site --no-actions

It will create SiteController.php file inside /src/Controller folder. Open controller file and write this code into it.



namespace App\Controller;

class SiteController extends AppController
    public function initialize(): void

    public function listEmployees()
        $employees = $this->Employees->find()->toList();

Inside above code, we have two methods –

  • initialize() method as constructor which loads model to class.
  • listEmployees() method responsible to load employees data into template file list_employees.php.

Create Template

Create Site folder inside /templates folder. Next, needs to create list_employees.php file inside /templates/Site folder.

Open list_employees.php file and write this following code into it. This will give the frontend layout for datatable.

<!DOCTYPE html>
<html lang="en">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
    <link rel="stylesheet" href="" />
    <link rel="stylesheet" href="" />


    <div class="container" style="margin-top:30px;">
        <h3 style="text-align: center;">Employees Report - DataTable Export Buttons by Online Web Tutor</h3>

        <div class="panel panel-primary">
            <div class="panel-heading">
                Employees Report
            <div class="panel-body">
                <table class="table table-striped" id="tbl-employees">

                        if (count($employees) > 0) {

                            $count = 1;

                            foreach ($employees as $emp) {
                                    <td><?= $count++ ?></td>
                                    <td><?= $emp['name'] ?></td>
                                    <td><?= $emp['email'] ?></td>
                                    <td><?= $emp['mobile'] ?></td>

    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>

        $(function() {
                dom: 'Bfrtip',
                buttons: [



Add Route

Open routes.php file from /config folder. Add this route into it.


    ['controller' => 'Site', 'action' => 'listEmployees']


Application Testing

Open terminal and run this command to start development server.

$ bin/cake server

URL: http://localhost:8765/employees

We hope this article helped you to learn about DataTable Data Export in CakePHP 4 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more