koolphp / koolreport

This is an Open Source PHP Reporting Framework which you can use to write perfect data reports or to construct awesome dashboards using PHP
https://www.koolreport.com/
MIT License
228 stars 65 forks source link

How to use KoolReport in Codeigniter? #4

Open koolphp opened 7 years ago

koolphp commented 7 years ago

Below are basic steps to integrate KoolReport into CodeIgniter:

  1. Copy the koolreport folder into codeigniter/application/libraries folder.
  2. Create application/reports folder where you report will be stored.
  3. 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.
  4. In above folder, create two files MyReport.php and MyReport.view.php. You may view the file below.
  5. 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; ?>

MyReport

MyReport

List all offices

$this->dataStore("offices"), "class"=>array( "table"=>"table table-hover" ) )); ?>
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:
![image](https://user-images.githubusercontent.com/8652590/29996660-d0cc0ad0-902c-11e7-9b1c-94e88ce53504.png)

![image](https://user-images.githubusercontent.com/8652590/29996662-e7af0194-902c-11e7-96ed-f4614c381a43.png)

![image](https://user-images.githubusercontent.com/8652590/29996664-f0d73bce-902c-11e7-8d1f-00352983c38d.png)
daN4cat commented 6 years ago

Question, why do you need to copy koolreport to codeigniter/application/libraries? Isn't composer and vendor supported?

koolphp commented 6 years ago

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

koolphp commented 6 years ago

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:

  1. How to make KoolReport use the databases settings from CodeIgniter?
  2. 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

wvaquerano commented 4 years ago

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"));
}

}?>` image

basirebadi commented 3 years ago

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.