IntroductionZend Framework (ZF) is a popular open source, MVC web application framework created and maintained by Zend Technologies, the company behind the PHP programming language.
This tutorial will walk you through the integration of Zend Framework 2 and phpGrid. It uses Zend Framework version 2.4 which requires PHP version 5.5 and above.
Install the Zend Framework Skeleton Application
Create your new ZF2 project:
composer create-project -n -sdev zendframework/skeleton-application path/to/install
“path/to/install” should be a folder in web root.
Before installing phpGrid, in the ZF2 that we created in the previous step, find the “vendor” folder. Create a new directory named “phpcontrols” inside. Then download phpGrid and extract the zip file into the “phpcontrols” folder we just created.
You should have folder structure similar to the following screenshot:
Configuring the conf.php file in phpGrid
Complete the phpGrid installation by configuring its database information in the “conf.php” file inside the phpGrid folder. For complete instructions on how to do this, see the phpGrid configuration online documentation.
phpGrid comes with several sample databases. You can find them under the “examples/SampleDB” folder. We will use the MySQL sample database for this Zend Framework integration tutorial.
Before start coding, we need to register our phpGrid library in the Zend Framework autoloader. This is done by adding autoload files keys in “composer.json”. The 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 vary slightly from what you have, and notice the autoload value.
"description": "Skeleton Application for ZF2",
Finally, once these changes have been made, we update the composer. In the project root, run the following command:
Open the file “module/Application/view/application/index/index.phtml“. Assuming that you have installed the phpGrid sample database installed,. Somewhere in “index.phtml”, add the following code:
$dg = new \C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
$dg -> enable_edit("INLINE", "CRUD");
$dg -> display();
Note that if you are working under a namespace while creating the object, you must use the “\” (root namespace), otherwise you will use the phpGrid class under the current namespace.
That’s all there is to it. You should now be able to run the demo.Run Demo
What about the controller and model?
You may be wondering, “Where is the ZF controller and model for phpGrid?” The answer is they are simply not required. phpGrid encapsulates both database access routines and display so you don’t have to worry about them. It does this magic “behind the scenes”.