drop down joomla login module
26
Thu, Dec
0 New Articles

Getting Started with Zend Framework on IBM i

Web Languages
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

You don't have to learn MVC to start using Zend Framework.

 

Recently, at the COMMON conference, I presented several PHP sessions. Two sessions were aimed toward attendees that are relative newcomers to PHP, and they were very well-received. To my thinking, it's time to raise the bar. While many of you are still just learning PHP and are more in the "testing it out" stage than doing actual development, there are a lot of developers on the IBM i who are writing real-world PHP applications. So this article is the first of what will be several focusing on transitioning from using plain PHP and the procedural model to harnessing the power of Zend Framework (ZF) and the Model-View-Controller (MVC) design pattern.

 

So now that I have frightened you, let me give you some good news. You don't have to learn the MVC design pattern to start using ZF. In fact, we are going to walk through a sample PHP script that uses some of the ZF tools and is still just a regular procedural script. But before we do that, let's cover a few of the basics.

Verify Your Zend Framework Version

In order to use ZF, you need to have Zend Core or Zend Server 5 for IBM i installed on your machine. Look for licensed program 1ZCORE5 for Zend Core or 2ZSVRPI for Zend Server. If you don't have one or the other of these installed, you'll need to install one. Either one can be downloaded from the Zend Web site. Both of the install packages include clear and concise directions for installing the software (usually just a *SAVF and RSTLICPGM command).

 

Once you verify that you have one of the PHP environments installed, you still need to verify that you have a recent version of Zend Framework (you probably don't). The most recent version as of this writing is 1.10.4. All the example scripts were written and tested using version 1.10.3, so any version 1.10.3 or later is fine. Many of you probably haven't updated ZF since you installed Zend Core or Zend Server. The versions installed with your PHP server automatically are as follows:

 

  • Zend Core: ZF version 1.6.0
  • Zend Server 5 for IBM i: ZF version 1.9.7

 

You can find out which version you're running by looking at the Zend Server GUI administration dashboard. The dashboard can be found at http://youribminame:10088/ZendServer. Once you open the dashboard, the ZF version is on the right side near the top (see below). This actually serves the dual purpose of both checking your version and making sure that your Zend Server subsystem is active.

 

081110OlenZF_version_snapshot_GUI

Figure 1: Check the dashboard to find the ZF version. (Click image to enlarge.)

 

If it's necessary, installing a new version of Zend Framework is easy. Download latest version from the Zend Web site. Then unzip the .zip file into the appropriate directory as shown below:

 

  • Zend Server users: /usr/local/zendsvr/share
  • Zend Core users: /usr/local/zend

 

Now you have a new subdirectory called ZendFramework-x.xx.x, where x.xx.x will be the version of the new installation. The easiest way to complete the installation and start using the new version is to rename the existing ZendFramework directory to ZendFramework_old. Then you can rename your newly installed subdirectory ZendFramework. You can now start using the new version without making any configuration changes.

 

Now that you're up to date with the ZF release, you can get down to business. Zend Framework is one of several "frameworks" available for PHP (CodeIgniter, Symfony, and CakePHP are a few others that are popular). Generally speaking, frameworks are packages of component-based tools/functions that are used in most development projects. For instance, Zend Framework has classes available that help you easily create forms (Zend_Form), authenticate users (Zend_Auth), easily access DB tables (Zend_Db), and more. Frameworks are also sometimes referred to as "scaffolding," which perhaps gives a more visual description of how they are used. Typically, these frameworks are tied to a specific design pattern; in the case of ZF, this is the Model-View-Controller (MVC) design pattern. For those of you unfamiliar with MVC, it can best be described as a standard design that all your applications are built on.

Your First Zend Framework Script

Using Zend Framework together with the MVC design pattern is the topic of a later article. For now, it's enough that you know what MVC is. We're actually going to use an alternative method to determine the installed ZF version for our first example script. The script uses one of the available ZF functions, and it gives me a decent alternative to the ubiquitous "Hello World" example.

 

<?php

require_once("Zend/Version.php");

echo "Zend Framework Version: ".Zend_Version::VERSION;

?>

 

The code above searches the include path for a directory/file called Zend/Version.php. This file contains a class called Zend_Version that in turn has a constant called VERSION. Then we use the echo construct to display the VERSION constant to the user. This is probably all review for most of you, so we'll move on to some other ZF functions.

Got Authentication?

One of the often-used and much-needed functions provided by ZF is the Zend_Auth classes. The Zend_Auth classes provide a tool that simplifies user authentication. In our example, we're going to use the Zend_Auth_Adapter_DbTable and Zend_Db classes. This is because we are going to use a DB2 table as our authentication database. What this means is that the user names and passwords we're expecting to receive from the Web user are stored on our IBM i in a DB2 table. It could be any table you specify. I have created a simple file called USERMSTR and included several fields in addition to the required "identity" column (aka user_profile) and "credential" column (aka password). The reason I included these additional columns will become clear later.

 

Here's the SQL to create the USERMSTR table:

 

create table jeffo/usermstr                     

 ( usermstr_id    integer   not null            

       as identity(no cache) primary key,       

   user_profile   varchar(10) not null,         

   password       varchar(15) not null,         

   realname       varchar(50) not null,         

   email_address  varchar(100) not null    )    

 

The code below is just for demonstration purposes. In a real-world example, you would want get the username and password from an HTML form (we'll get to that later too). For now, we just want to get a feel for how to use the ZF classes.

 

<?php

//-----------------------------------------------------------------------------

// Set to display errors - TESTING ONLY!!

//-----------------------------------------------------------------------------

ini_set('display_errors', '1');

//-----------------------------------------------------------------------------

// Use Autoloader to include the Zend Framework Classes we are going to use.

//-----------------------------------------------------------------------------

require_once 'Zend/Loader/Autoloader.php';

Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);

//-----------------------------------------------------------------------------

// Set up the DB options

//-----------------------------------------------------------------------------

$library = 'jeffo';

// single database library option

$driverOptions = array( "i5_lib" => $library);

// Library list option

/* $driverOptions = array( "i5_naming" => DB2_I5_NAMING_ON,

         "i5_libl" => "TBSCOMMON JMOQA2 JMOQA1 JMOPROD QGPL");  */

$config = array( "host" => "localhost",

                 "username" => "quser",

                 "password" => "quser",

                 "dbname" => "LEGATO3",

                 "driver_options" => $driverOptions);

if (!$db = Zend_Db::factory('DB2', $config)) {

       echo "didn't create the DB adapter<br>";

       die();

}

//----------------------------------------------------------------------------

// Setup the authentication database adapter

//----------------------------------------------------------------------------

if (!$authAdapter = new Zend_Auth_Adapter_DbTable($db)) {

       echo "didn't create the Zend Auth Adapter table object<br>";

       die();

}

// set the table and columns to use for authentication

$authAdapter

    ->setTableName('USERMSTR')

    ->setIdentityColumn('USER_PROFILE')

    ->setCredentialColumn('PASSWORD')

;

// set the values (usually from a login form) to be authenticated

$authAdapter

    ->setIdentity('username')

    ->setCredential('password')

;

// authenticate using the specified credentials (specified above)

$authResult = $authAdapter->authenticate();

// display authentication results

switch ($authResult->getCode()) {

      

       case Zend_Auth_Result::SUCCESS:

              echo "User successfully logged in";

              // print the returned contents of the result row in the

             // authentication table

              print_r($authAdapter->getResultRowObject());

              break;

             

       case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:

        echo "User Identity not found. Login unsuccessful";

        break;

    case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:

        echo "User/Password combination not valid. Login unsuccessful";

        break;            

       

       default:

              echo "Login unsuccessful. Unidentified error";

}

?>

 

The first thing we do in the code is set the display_errors flag to 1. This will allow us to see any errors that occur during the execution of our script, which can be helpful during testing, but you must remember to remove it before moving into production because the information displayed can potentially be sensitive information (names of databases, variable names, etc.). Next, we use the require_once function, which includes other source code similar to a /copy in RPG. In this case, we're including the ZF Autoloader class (/Zend/Loader/Autoloader.php). The Autoloader class provides some advanced functionality that we don't necessarily want to address here. In the case of our sample script, we're using the Autoloader to avoid the trouble of coding a require_once for each ZF class we use. By using the following code, we can use any ZF classes in our script, and the PHP interpreter will "autoload" the class for us.

 

require_once 'Zend/Loader/Autoloader.php';

Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);

 

It's a convenience tool. The Autoloader class is really beyond the scope of this article, but you can find out more about it in the Zend Framework Manual.

 

Once we have the Autoloader code in place, we move on to configuring and creating the ZF database adapter. We have ability here to code a single database library (as I have done in the example).

 

$library = 'jeffo';

// single database library option

$driverOptions = array( "i5_lib" => $library);

 

Or we can use a library list by changing the code above to read as shown below:

 

// Library list option

$driverOptions = array( "i5_naming" => DB2_I5_NAMING_ON,

         "i5_libl" => "TBSCOMMON JMOQA2 JMOQA1 JMOPROD JEFFO QGPL"); 

 

Then we use an array ($config) to set the database connection parameters. These parameters should be familiar as they are nearly the same as the parameters we have used previously with the db2_connect function. The difference is that for the ZF database class, we're placing the key/value pairs in an array instead of passing them as individual parameters as we do on the db2_connect function. Once we populate the configuration array, we use the Zend_Db class to create a database object.

 

if (!$db = Zend_Db::factory('DB2', $config)) {

       echo "didn't create the DB adapter<br>";

       die();

}

 

There are several ways we could create the database adapter object ($db). We use the static method Zend_Db::factory to create our adapter. Without getting too esoteric, a factory method is a generic method that creates instances of similar objects. For example, we can use the Zend_Db::factory method to create database adapters objects for DB2, MySQL, and SQLite.

 

Once the database adapter object is created, we need to set up Zend_Auth so that it knows to use that database adapter for authentication requests. This is done by creating a Zend_Auth_Adapter_DbTable object and passing our existing database adapter object as a parameter to the __construct method.

 

if (!$authAdapter = new Zend_Auth_Adapter_DbTable($db)) {

       echo "didn't create the Zend Auth Adapter table object<br>";

       die();

}

 

Now that we've created the Zend_Auth_Adapter_DbTable object ($authAdapter), we need to configure it to our specific database.

 

// set the table and columns to use for authentication

$authAdapter

    ->setTableName('USERMSTR')

    ->setIdentityColumn('USER_PROFILE')

    ->setCredentialColumn('PASSWORD')

;

 

First, we configure Zend_Auth to use the correct table and columns for authentication. In this case, we'll be using database table USERMSTR and using the USER_PROFILE and PASSWORD columns to authenticate on. As I said before, you would normally expect to receive the input for authentication from a login Web page. For now, we're just hardcoding the USER_PROFILE and PASSWORD values we're authenticating.

 

// set the values (usually from a login form) to be authenticated

$authAdapter

    ->setIdentity('username')

    ->setCredential('password')

;

 

Now all that's left is to actually do the authentication. This is accomplished using the aptly named "authenticate" method.

 

// authenticate using the specified credentials (specified above)

$authResult = $authAdapter->authenticate();

 

The authenticate method returns an object with different properties and values, depending on the result of the authentication. Some of the results can be seen in our code. For instance, we're using the getCode method to retrieve the authentication result code and then comparing that to the ZF Zend_Auth_Result constants to determine the actual results.

 

// display authentication results

switch ($authResult->getCode()) {

      

       case Zend_Auth_Result::SUCCESS:

              echo "User successfully logged in";

              // print the returned contents of the result row in the

             // authentication table

              print_r($authAdapter->getResultRowObject());

              break;

             

       case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:

        echo "User Identity not found. Login unsuccessful";

        break;

    case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:

        echo "User/Password combination not valid. Login unsuccessful";

        break;            

       

       default:

              echo "Login unsuccessful. Unidentified error";

}

 

Remember I said that there was a reason that I included some columns in the USERMSTR table other than just the USER_PROFILE and PASSWORD columns required for authentication? Well, now we get to see why. One of the other features of Zend_Auth is that if the authentication is successful, you can retrieve the contents of the retrieved authentication row (the row from the USERMSTR table). This is done using the getResultRowObject as shown above. Since you will probably want to store other authorization-type information in the USERMSTR table, the getResultRowObject can be really handy.

 

You can see how easy it would be to modify this code to accept input from a form. Also, to make things more secure, you could store your passwords using the MD5 encryption available in PHP. In the next article, we'll cover how to use Zend_Form to create a login page and tie into the sample code we created here.

 

Editor's note: Zend Server 5.0.2 is now available for IBM i.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: