How to use KoolReport in Codeigniter?
Below are basic steps to integrate KoolReport into CodeIgniter:
- Copy the koolreport folder into codeigniter/application/libraries folder.
- Create application/reports folder where you report will be stored.
- Create folder assets in the same level with folder application. Or if you have public assets folder where css and js file is hold then there is no need. But assume that you have assets folder that I suggested to create.
- In above folder, create two files MyReport.php and MyReport.view.php. You may view the file below.
- In the application/controllers/Welcome.php, you do:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH."/reports/MyReport.php";
class Welcome extends CI_Controller {
public function index()
{
$report = new MyReport;
$report->run()->render();
}
}
<?php
//MyReport.php
require APPPATH."/libraries/koolreport/autoload.php";
class MyReport extends \koolreport\KoolReport
{
use \koolreport\clients\Bootstrap;
function settings()
{
return array(
"assets"=>array(
"path"=>"../../assets",
"url"=>"assets",
),
"dataSources"=>array(
"automaker"=>array(
"connectionString"=>"mysql:host=localhost;dbname=automaker",
"username"=>"root",
"password"=>"",
"charset"=>"utf8"
)
)
);
}
function setup()
{
$this->src('automaker')
->query("Select * from offices")
->pipe($this->dataStore("offices"));
}
}
<?php
//MyReport.view.php
use \koolreport\widgets\koolphp\Table;
?>
<html>
<head>
<title>MyReport</title></title>
</head>
<body>
<h1>MyReport</h1>
<h3>List all offices</h3>
<?php
Table::create(array(
"dataStore"=>$this->dataStore("offices"),
"class"=>array(
"table"=>"table table-hover"
)
));
?>
</body>
</html>
All done! The KoolReport has been successfully in CodeIgniter project. Couple of explanation regarding to assets folders:
Since the KoolReport contains css/js needed to be accessed. If the koolreport folder is inside application folder of codeigniter, there is no way that those css/js can be accessed due to the rule of CI. That's why we have the assets settings in the settings() function of MyReport:
"assets"=>array(
"path"=>"../../assets",
"url"=>"assets",
),
This will direct KoolReport to copy all css/js to the assets folder so that they can access to. The path is the relative path from MyReport.php to the public assets folder. And the url is the link to the assets folder in browser. In this case, assets folder is in the same folder with index.php so we can simplly set "assets".
Some image to illustrate:



Question, why do you need to copy koolreport to codeigniter/application/libraries? Isn't composer and vendor supported?
Yes, KoolReport support installing through composer, if you installed through composer you do not need to copy files or require the "autoload.php" of KoolReport
We have create CodeIgniter package to make KoolReport work seamlessly inside CodeIgniter. While KoolReport is working well with CodeIgniter from the beginning, however there are two issues that most people asked:
- How to make KoolReport use the databases settings from CodeIgniter?
- How to publish the KoolReport's widget resources files to public folder?
So this new package will help you to do above things with a simple line of code:
class MyReport extends \koolreport\KoolReport
{
use \koolreport\codeigniter\Friendship;// All you need to do is to claim this friendship
function setup()
{
//Now you can access database that you configured in codeigniter
$this->src("sale_database")
->query("select * from orders")
->pipe($this->dataStore("orders"));
}
}
Simple, isn't it?
The package is TOTALLY FREE so you can just download it. FYI, we also create the same package for Laravel as well. We plan to add more packages like this for other PHP Frameworks like Symfony, CakePHP, Yii2 etc.
Hope that helps.
Regards
Hi @koolphp I have installed using composer both KoolReport and CodeIgniter packages. I created the reports folder in the application and used friendship but CodeIgniter is not finding the kool report class. `<?php class MyReport extends \koolreport\KoolReport { use \koolreport\codeigniter\Friendship;// All you need to do is to claim this friendship //use \koolreport\clients\Bootstrap;
function setup()
{
//Now you can access database that you configured in codeigniter
$this->src("recursoshumanos")
->query("select * from licenciaporempleado")
->pipe($this->dataStore("licencias"));
}
}?>`

Please replace the bellow path in MyReport.php file, it will work.
require APPPATH."/libraries/koolreport/autoload.php"; (old)
require APPPATH."libraries\koolreport\core\autoload.php"; (replace with this one).
thanks.