To create a report using Smart Report Engine, regardless of the report type, you need to follow these steps:

  1. Import two classes from Smart Report Engine, namely “ReportOptions” and “CustomEngine.” The “ReportOptions” class is responsible for defining all the necessary report options, while the “CustomEngine” class is responsible for generating the report.
  2. Create an instance of the “ReportOptions” class and use it to specify all the required report options.
  3. Instantiate the “CustomEngine” class by calling its constructor and passing the “ReportOptions” instance from the previous step as a parameter.
  4. Call the “Create_report” method, which is a member of the “CustomEngine” class, to generate the report and retrieve its URL.
  5. You can either display the URL returned from the previous step as a link to your users or redirect them directly to that URL.

In this tutorial, we will demonstrate how to build a PHP report based on a single MYSQL table, with minimal options to showcase the primary usage of this powerful PHP reporting framework. For this example, let’s assume we have an “items” table in an MYSQL database that stores data about items in a sunglasses store. We will provide the code for a native PHP project, as well as a Laravel project.

1
2
3
4
5
6
7
8
9
10
11
12
13
use SRE\Engine\CustomEngine;
use SRE\Engine\ReportOptions;
 
require_once "../sre_bootstrap.php";
$report = new ReportOptions(SRE_PUBLIC_REPORT);
$report->select_tables(array("items"))
       ->set_grouping(array("country"))
       ->select_all_fields();
$engine = new CustomEngine($report);
$report_path = $engine->create_report();
if ($report_path) {
    echo "Report created successfully! To visit your report please <a href='" . $report_path . "' /> click here </a> ";
}

2- Case of Laravel Project

The logic for generating the report will be encapsulated in a service, which will resemble the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace App\Services;
 
use Sre\SmartReportingEngine\src\Engine\Constants;
use Sre\SmartReportingEngine\src\Engine\CustomEngine;
use Sre\SmartReportingEngine\src\Engine\ReportOptions;
 
class ReportService {
 
    public function generatePublicReport() {
 
        $report = new ReportOptions(Constants::SRE_PUBLIC_REPORT);
        $report->select_tables(array("items"))
               ->set_grouping(array("country"))
               ->select_all_fields();
        $engine = new CustomEngine($report);
        $report_path = $engine->create_report();
        return $report_path;
    }
}

The variance between utilizing this PHP reporting framework in a native PHP project versus a Laravel project lies in the utilized package, paths, and autoloading methods.

Notes

  • If you are not employing composer for autoloading, you will need to include the “sre_bootstrap.php” file, which is located in the downloaded Smart Report Engine package at “/Native/sre_bootstrap.php”.
  • We are presuming that the “sre_bootstrap.php” file exists in a directory one level higher than the present directory.

Report Options

The provided code starts by defining the necessary report options using the “$report” object, which is an instance of the “ReportOptions” Class. To set the report as public, we pass the “SRE_PUBLIC_REPORT” flag to the constructor of the “ReportOptions” class. All flags used in the Smart Report Engine start with “SRE_.”

To specify the data source of the report, we select a particular MYSQL table, in this case, “items,” and pass an array with only that table to the “select_tables” method. We can also group the report by a specific column by passing an array with only that column to the “set_grouping” method, which is done in the above example with the “country” column.

Using Smart Report Engine, we can select some or all columns of the selected table. In the provided code, we want to choose all columns, so we call the “select_all_fields” method.

After defining all the report options, we create a new object of the CustomEngine class by passing the instance of the ReportOptions class to its constructor. We can then call the “create_report” method to generate the report and return its URL.

If we are working on a native PHP project, we can find the generated report at “/sre-reports/rep-your-report-timestamp/rep-your-report-timestamp.php”. In a Laravel project, the report is located at “/public/sre_reports/rep-your-report-timestamp/rep-your-report-timestamp.php.”

Summary

In conclusion, Smart Report Engine is a powerful reporting framework that can be used to create professional reports in native PHP or Laravel projects.