This tutorial for beginner cakephp developer who just started their journey with cakephp framework. Here i am going to cover how to create component and call it to your controller.
Component is small chunk of utility code to perform common action on your cakephp controllers.
Cakephp already has some default component but you can also create your own customize component very easily. Creating components keeps controller code clean and allows you to reuse code between multiple controllers.



In this example i am going to create a simple component to export mysql data into excel.

Step-1: Go to app/Controller/Component and create file name ExportXlsComponent.php

Step-2: Write basic structure of your component, Your class name and file name should be same and in camel case.

<?php
App::uses('Component', 'Controller');
class ExportXlsComponent extends Component {
 
 
}
?>

Step-3: Put your common mysql to excel export function in this component.

function export($fileName, $headerRow, $data) {
 ini_set('max_execution_time', 1600); //increase max_execution_time to 10 min if data set is very large
  $fileContent = implode("\t ", $headerRow)."\n";
  foreach($data as $result) {
   $fileContent .=  implode("\t ", $result)."\n";
  }
 header('Content-type: application/ms-excel'); /// you can set csv format
 header('Content-Disposition: attachment; filename='.$fileName);
 echo $fileContent;
exit;
}

Now your complete component file will be

ExportXlsComponent.php

<?php
App::uses('Component', 'Controller');
class ExportXlsComponent extends Component {
 
function export($fileName, $headerRow, $data) {
 ini_set('max_execution_time', 1600); //increase max_execution_time to 10 min if data set is very large
  $fileContent = implode("\t ", $headerRow)."\n";
  foreach($data as $result) {
   $fileContent .=  implode("\t ", $result)."\n";
  }
 header('Content-type: application/ms-excel'); /// you can set csv format
 header('Content-Disposition: attachment; filename='.$fileName);
 echo $fileContent;
 exit;
 }
}
?>



Step-4: Finally your component has been created successfully now time to use it in your controller, Suppose you have BooksController and you want to export your all mysql books data into excel then you can use your newly created custom component.

Fist you need to call your component in your controller using below method in controller

public $components = array('ExportXls');

After that you can access your all component function in your given controller.

BooksController.php

<?php
class BooksController extends AppController {
 
public $components = array('ExportXls');
 
 function reports() {
  $this-autoRender = false;
  $this->layout = false;
$fileName = "bookreport_".date("d-m-y:h:s").".xls";
//$fileName = "bookreport_".date("d-m-y:h:s").".csv";
$headerRow = array("Book Title", "ISBN No.", "Auther");
$data = array(
           array('Book Title1', '1111111111', 'Rohit Kumar-1'),
           array('Book Title2', '2222222222', 'Rohit Kumar-2'),
           array('Book Title3', '3333333333', 'Rohit Kumar-3'),
           array('Book Title4', '4444444444', 'Rohit Kumar-4') 
          );
  $this->ExportXls->export($fileName, $headerRow, $data);
  }
}
?>

If you like this post please don’t forget to subscribe my public notebook for more useful stuff