Click here to Skip to main content
15,881,380 members
Articles / All Topics

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Nov 2015CPOL2 min read 5K  
In the previous phpGrid Laravel 5 tutorial part 1, we have chosen to leave the phpGrid code inside route.php. In this tutorial, we will improve upon what we have learned in part 1.

In the previous phpGrid Laravel 5 tutorial part 1, we have chosen to leave the phpGrid code inside route.php. In this tutorial, we will improve upon what we have learned in part 1. We will move the phpGrid folder out of public directory and move it to app folder instead; and then we will undo changes in route.php and add phpGrid code inside controller file.

Let’s get started.

 

Move phpGrid folder into Laravel \app folder

 

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

Make sure to udpate SERVER_ROOT value in conf.php. See below folder structure

phpgrid laravel5 part2 folder structure

 

Undo changes made in route.php

 

In route.php, undo the dashboard route changes back we made in Part I back to default. We will be moving the phpGrid code to controller as it is the properly place to organize your application behaviors.

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

 

A better way to include an external library in Laravel 5

 

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

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

Modify composer.json

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

Below is a copy of our composer.json. It could be slightly from what you have, and 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"]
    },

Notice: if you make these changes to 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 DashboardController.php in “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

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

The post phpGrid, Laravel 5 Integration – Part II (Improved version) appeared first on phpGrid.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
He likes programming and iPod.

Comments and Discussions

 
-- There are no messages in this forum --