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

phpGrid and Zend Framework Integration

Introduction

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

The best way to create a Zend Framework project is with Composer. Start by using Composer to install Github’s ZendSkeletonApplication. It’s a great starting point to begin a blank Zend project.

Create your new ZF2 project:

1
composer create-project -n -sdev zendframework/skeleton-application path/to/install

“path/to/install” should be a folder in web root.

Install phpGrid

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:

zendframework-folder

 

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.

Modify “composer.json”

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.5",
        "zendframework/zendframework": "~2.5"
    },
    "autoload":{
        "files": ["vendor/phpcontrols/phpGrid/conf.php"]
    }
}

Finally, once these changes have been made, we update the composer. In the project root, run the following command:

1
composer update

Start coding!

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:

1
2
3
$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”.