image-zend-blog-unleash-DB2-databases
February 21, 2020

Unleash Your Db2 Databases with PHP

IBM i
PHP Development

Within the IBM i community, we often talk about modernization. Regardless of approach, the goals of every modernization project are based on making it easier to consume the golden gems contained in Db2 databases: 

  • Through web forms/pages that allow for more dynamic and deeper displays of information in Db2 databases.
  • By leveraging the web to make Db2 data available to a wider set of constituents, stakeholders, and customers.  

This blog explores different methods for creating web-based applications written in PHP that can work with your IBM i data and provide CRUD functionality.

If you've not heard the term CRUD before, it stands for Create, Read, Update, and Delete. A CRUD-based application can use these basic Db2 SQL commands for retrieving, updating, and manipulating information in Db2 databases and other data stores. I’ll review the three most common approaches for implementing a PHP, CRUD-based application including their advantages, disadvantages, and level of complexity. They include using:

  • Custom PHP scripts.
  • MVC middleware/frameworks, specifically Laminas (formerly Zend Framework).
  • phpGrid.
zend-blog-db2-data-functions-supported

Custom PHP Scripts

The first approach to talk about is developing your own custom PHP scripts. For this approach, you need to:

  • Develop separate scripts that handle each of the four functions of CRUD.
  • Link the scripts, as needed, via the html 'href' attribute.  

Typically, the scripts manage the logic, data presentation, and user-input gathering via HTML. So, when you use this approach, the business logic will not be separate from the data representation.  

While one monolithic script could be used, a more flexible approach would be to separate the main functions into separate scripts. For example, using CRUD commands, you could write a:

  • Create script that presents an HTML form that collects data and adds a record to the database/table.
  • Read script that provides a form that can be used to input search criteria that selects the requested records. This script, and more specifically the resulting list of records, could be used as the "input" for the Update and Delete functions likely using an 'href' against the key field of each returned record.
  • Update script that selects a specific record or form, and populates it with the field-data from the selected record.  An <Update> function would perform an SQL UPDATE function, programmatically from PHP, using the data from the update form.
  • Delete script that deletes the selected record using a SQL DELETE statement. To avoid errors, it’s a good idea to include a deletion confirmation step in your delete scripts.

Pros and Cons of Custom PHP Scripts

This would certainly not be the most rapid approach, since you need to develop custom scripts for each function.  Additionally, since no framework is being used, it is likely that functions — such as connecting to the data store — would be implemented several times within the scripts. Not only does this lead to high levels of code repetition, but also, it can create a difficult situation for maintenance down the road. Imagine having 50 instances and having to change one of these functions? 

Additionally, to provide forms that are appealing to users and representative of data formats requires the retrieval of data-attribute information, which adds to script complexity.  And if the data attributes change, you need to change the various forms used to display and manipulate data, possibly in multiple places throughout the scripts.

Third Party Applications – PHPgrid

Another approach for developing PHP applications that can interact with your Db2 databases via CRUD functions is to use a third-party solution, such as PHPgrid.  

The Pros and Cons of PHPgrid

The advantage of using a solution such as PHPgrid, is that it removes much of the minutia you’ll have to manage when you develop custom scripts. In addition, it can be easier to provide a relatively clean display of your data using basic CRUD functionality. For example:

<!DOCTYPE html>
<?php
use phpgrid\C_DataGrid;
require_once("phpGrid/conf.php");
$dg = new C_DataGrid("SELECT * FROM SP_CUST","CUST_ID","SP_CUST");
$dg -> display();
?>

Along with a corresponding configuration file that sets items such as the DBMS (Db2), the database host, and library, the above code snippet is a complete PHP script that provides the following data grid:

zend-db2-blog-data-grid

And, it is relatively simple to add additional CRUD functionality. All you need to do is add the following lines to your script:

  • Search:  $dg -> enable_search(true);
  • Create, Update, Delete:  $dg -> enable_edit("FORM");
  • Master/Detail:  
$sdg = new C_DataGrid("SELECT * FROM ORDER", "CUST_ID", "ORDERS");
$dg->set_masterdetail($sdg, "CUST_ID");

The disadvantage of developing PHP applications using PHPgrid is that it can be challenging to evolve or scale the application to deliver new functionality. For example, it could be difficult to add a function that uses the records retrieved via PHPgrid as input to an RPG call (via the iToolkit/XMLService).  

MVC Middleware/Frameworks — Specifically Laminas

Another option for developing PHP applications that run on your IBM i systems, and interact with your Db2 databases is to develop PHP applications that leverage a framework and middleware, such as the Laminas Project (formerly Zend Framework) and its corresponding middleware solution, Mezzio (formerly Expressive). Read this blog for more information about the Zend Framework’s transition to Laminas.

If you have been around PHP development for a while, you probably know all about frameworks. If you haven't been exposed to frameworks, don't worry. Here are some definitions that will help: 

  •  A framework provides a platform for streamlining the development of web applications.
  • Middleware are applications that sit between applications to pre-process incoming requests and post-process outgoing responses.

MVC stands for Model, View, Controller. It is a design paradigm or pattern for managing the requests of web-based applications by separating the:

  • Application’s model or business logic that govern data manipulation.
  • View or user interface.
  • Controller, which uses the model to process all data received and displayed by the user interface, or view, including GET calls, POST data, and uploaded files.
zend-blog-db2-data-mvc

It’s worth noting that you would use either MVC or middleware to manage your web application’s requests. 

Benefits of Frameworks

You can simplify and streamline the development of web-based, PHP applications that run on the IBM i platform by using frameworks. They eliminate the need to develop the same solution or write the same code over and over again. Frameworks also establish connections between your application and data sources.

Most frameworks, including Laminas, adhere to the Model-View-Controller (MVC) design pattern. The concept behind MVC is to isolate the business logic of an application from user interface considerations.  By isolating the business logic from the user interface, it becomes easier to modify either without affecting the other. 

Laminas 

The Laminas Project is the new name of Zend Framework! While Zend by Perforce continues to be a major influencer and developer of the Laminas Project, it was recognized that the framework would benefit from an open governance model, and was therefore transferred to the Linux Foundation as the Laminas Project. It is important to keep in mind that Laminas is not a new framework but rather a continuation of the Zend Framework, which has been continuously developed by an extensive community over the last 15 years — and downloaded 400 million times. Additionally, it brings extensibility and testability to PHP applications.

Mezzio

If you prefer to use middleware, which is a different and more flexible approach than MVC, you can use Mezzio. It is a subproject of Laminas that provides middleware for building PHP applications that use the PSR-7 (HTTP Messages) and PSR-15 (HTTP Request Handlers) specifications along with a variety of routing and templating options. 

PSR stands for PHP Standard Recommendation. PSRs are interfaces or APIs produced by the PHP Framework Interop Group (PHP-FIG) for the purpose of standardizing common application concerns within PHP frameworks and applications. Middleware such as Mezzio is driven, in part, through implementation of various PSRs. 

When it comes to developing CRUD applications, the PSRs that matter include:

  • PSR-7 (HTTP Messages): APIs that define HTTP messages, including requests, responses, URIs, and file uploads.
  • PSR-15 (Request Handlers and Middlware): APIs that define request handlers and middleware that consume PSR-7 server-side requests and produce PSR-7 responses. PSR-15 enables the separation of common requests and the processing of responses from applications’  business logic.
  • PSR-17 (HTTP Factories): APIs for defining factories, allowing middleware to be written agnostic of PSR-7 implementations. This allows changing the underlying PSR-7 implementation when needed.

So why would we want to use middleware? Put simply, middleware:

  • Simplifies the creation of custom workflows with every endpoint. 
  • Speeds response times for users and applications by creating only the objects needed for the specific request.
  • Simplifies application development, testing, and maintenance by supporting abstraction that helps to build more modular applications — and simplify CI/CD workflows, even in distributed environments.

Frameworks such as Laminas rely on Composer to install and update applications’ various components.  When the application structure is first created, a directory structure such as the following is created:

zend-blog-db2-data-composer-code

Structure items include: 

  • Handler — processes (handles) a request and produces a response as defined by PSR-7.
  • Interface — defines required methods (functions).
  • Factory — an object that creates other objects and provides what they need to complete their work.
  • ConfigProvider — determines configuration for the application module.
  • Table — deals with data access/db2.
  • Customer— represents a single customer.
  • CreateCustomerFromArray — a class that creates a customer instance from an array of values.

Creating Middleware with Mezzio

At a very high level, to create a Mezzio application, you will first use Composer to create the project:

composer create-project mezzio/mezzio-skeleton mezzio

After answering a few questions regarding interfaces for routing and templating, you can start writing your middleware. Generally, the default responses suffice, except when it comes to an engine. You should specify one because the default is to omit it. 

For examples of middleware code, please see this document.

Conclusion

Developing web-based PHP applications that leverage your Db2 databases using CRUD — or Db2 SQL commands — open up extensive opportunities for modernization, ROI, and improved user services. Not only can you unlock your data silos so that you make use of historic and current data, but also, you can deliver new services that people take advantage of using their desktops and mobile devices.

Regardless of which method you choose to develop your PHP apps — custom PHP scripts, phpGrid, frameworks such as Laminas, or middleware such as Mezzio — Zend can help. Our PHP and Laminas experts can help you with your projects by providing:

Questions?

CONTACT US

Additional Resources