Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

phpGrid and Zend Framework Integration

5.00/5 (1 vote)
13 Sep 2015CPOL2 min read 7.8K  
phpGrid and Zend Framework Integration

zf2-phpgrid

Introduction

This tutorial will walk you through the integration of the Zend Framework 2 and phpGrid. Zend Framework (ZF) is a popular open source, MVC web application framework created and maintained by Zend Technologies, the company behind PHP programming language.

Install Zend Framework Skeleton Application

The best way to create a Zend Framework project is to use Composer. This tutorial uses Zend Framework version 2.4 that requires PHP version 5.5 and above. In this tutorial, you will start by installing the ZendSkeletonApplication from Github using Composer. It’s a great starting point to begin a blank Zend project.

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.

Install phpGrid

Before installing phpGrid, in the ZF2 that we installed in the previous step, find the “vendor” folder, and 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 conf.php File in phpGrid

Complete the phpGrid installation by configuring its database information in file “conf.php” inside phpGrid folder. For complete instructions on how to do this, see phpGrid configuration online documentation. phpGrid comes with several sample databases. You can find them under “examples/SampleDB” folder. We will use the MySql sample database for Zend Framework integration tutorial.

Modify composer.json

Before we start coding, we need to register our phpGrid library in Zend Framework 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.

{
    "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, update composer after making the changes. In the project root, run the following:

composer update

Start Coding!

Open file “module/Application/view/application/index/index.phtml“. Assuming that you have installed phpGrid sample database installed, somewhere in index.phtml, add the following:

$dg = new \C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
$dg -> enable_edit("INLINE", "CRUD");
$dg -> display();

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.

That’s all there it is. You should be able to run the demo.

Run Demo

What About the Controller and Model?

You are probably 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 that magic “behind the scenes”.

The post phpGrid and Zend Framework Integration appeared first on phpGrid.

License

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