Files needed for this demo
- phpGrid Lite (free)
- Symfony 3 (free)
Where to keep phpGrid files in Symfony (Hint: not in “vendor” folder)
The short answer is “web” folder. Symfony web assets are used to keep things like CSS, JavaScript and image files that renders the front-end of your site. phpGrid encapsulates both database access routines and display so you don’t have to worry about them. It does this magic “behind the scenes”.
Edit conf.php in phpGrid
Remember to change the database credentials in conf.php. You do not need to set SERVER_ROOT value because phpGrid is inside public accessible “web” folder.
Enable PHP templating engine
Before we start phpGrid Symfony integration, make sure the “php” templating engine is enabled in your application configuration file under app/config .Symfony defaults to Twig for its template engine, but you can still use plain PHP code if you want. Symfony adds some nice features on top of PHP to make writing templates with PHP more powerful.
1 2 3 4 5 | # app/config/config.yml framework: # ... templating: engines: ['twig', 'php'] |
Create Symfony Controller
Symfony Controller needs to call our grid view file because we do not want to use Controller to render our datagrid. In this demo, we will modify the default controller located in folder “src/AppBundle/Controller/”. For simplicity, we keep the route path the same as the view folder structure.
1 2 3 4 5 6 7 8 | /** * @Route("/phpgrid/simple") */ public function gridAction(){ return $this->render('phpgrid/simple.html.php'); } |
Create Symfony View file
First of all, create a new folder named “phpgrid” in “app/Resources/views”. Secondly, create a view file named “simple.html.php”, or whatever view file name used in your Controller in the previous step.
Include the phpGrid config file.
1 2 | $webDir = $this->container->getParameter('kernel.root_dir'); require_once($webDir ."/../web/phpGrid_Lite/conf.php"); |
Load a simple datagrid from phpGrid sample database table “orders” (The sample database is located under examples/SampleDB)
1 2 | $dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders"); $dg->display(); |
Congrats! You’ve just created your first datagrid in Symfony! Now visit “your_domain.com/phpgrid/simple” to see the datagrid.