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





0/5 (0 vote)
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
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 Demoreference:
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.