877-GRID-666 24/7 online support 604, Tower A, New Trade Plaza, N.T., Hong Kong

phpGrid, Laravel 5 Integration – Part II (Improved version)

In phpGrid Laravel 5 Tutorial Part 1, we chose to leave the phpGrid code inside “route.php”. In this tutorial, we will improve upon what we learned in Part 1. We will move the phpGrid folder out of the public directory and move it to the “\app” folder instead. Then, we will undo changes in “route.php” and add phpGrid code inside the controller file.

Let’s get started.

 

Move the phpGrid folder into the Laravel “\app” folde

 

Technically, you can put a class anywhere you like within the “\app” folder. I personally prefer to create a separate one such as “app\Libraries” for any external libraries.

Make sure to update the “SERVER_ROOT” value in “conf.php”. See folder structure below:

phpgrid laravel5 part2 folder structure

 

Undo changes made in “route.php”

 

In “app/Http/route.php”, UNDO the changes we made in Part I back to default. We will be moving the phpGrid code to the controller as it is the proper place to organize your application behaviors.

1
Route::get('dashboard', 'DashboardController@index');

 

A better way to include an external library in Laravel 5

 

It’s possible to include PHP classes even without a namespace in the controller. Strictly speaking, we will be using the “\” (root namespace) as the default namespace.

First of all, let’s get our autoloader to work.

Modify “composer.json”

Before we start coding, we need to register our phpGrid library in the Laravel autoloader by adding the autoload files keys in “composer.json”. Autoloader ensures that any PHP external libraries and components can be easily referenced anywhere in the PHP code without using the traditional require or php include function.

Below is a copy of our “composer.json”, which may differ slightly from what you have. Notice the “autoload” value.

1
2
3
4
5
6
7
8
9
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\": "app/"
        },
        "
files": ["app/Libraries/phpGrid_Lite/conf.php"]
    },

Note: if you make these changes to the “composer.json” file, don’t forget to run “composer dump-autoload” or “composer update” for changes to take effect.

1
composer dump-autoload

– OR –

1
composer update

 

Modify “DashboardController.php”

 

Open the file “DashboardController.php” in the “app\Http\Controllers” folder, and change the index() function to the following. Note that if you are under a namespace while creating the object, you must use the “\” (root namespace), otherwise you will use the phpGrid class under the current namespace.

1
2
3
4
5
6
7
8
9
10
11
12
13
public function index()
{
    $dg = new \C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
    $dg->enable_edit("FORM", "CRUD");
    $dg->enable_autowidth(true)->enable_autoheight(true);
    $dg->set_theme('cobalt-flat');
    $dg->set_grid_property(array('cmTemplate'=>array('title'=>false)));
    $dg->display(false);

    $grid = $dg -> get_display(true);

    return view('dashboard', ['grid' => $grid]);
}

That’s all there it is. You should be able to run the demo.

Run Demo Download Demo

Technical reference:
http://laraveldaily.com/how-to-use-external-classes-and-php-files-in-laravel-controller/