PHP Employee Data Demo

The PHP based employee data demo (zero.phpemployee.demo) is a RESTful application developed using the PHP programming language that demonstrates many of IBM® WebSphere® sMash's capabilities. It provides an interface for managing employee , including listing, creating, editing and deleting employee records.

Features of the application

This application makes use of the following features:

  • Resource handling
  • PHP scripting
  • Data access
  • JSON APIs and rendering
  • Client programming with Dojo

For the following overview we assume you and are familiar with terms such as REST and JSON.

Installing and running the PHP based employee data demo (Command Line)

Use the following steps to install the demo with the command line:

  1. Install the WebSphere sMash command line utility using the instructions on the WebSphere sMash Web site.
  2. Install the application from the WebSphere sMash repository using the following command:
    zero create php.demo from zero:zero.phpemployee.demo
  3. Navigate to the php.demo directory.
    cd php.demo
  4. Create the database artifacts by running the following command from the root directory of the project (php.demo):
    zero runsql setup_db.sql
  5. Start the application with the following command:
    zero start
  6. After following the previous steps, you can interact with the application in the following ways:
    Dojo-based UI
    http://localhost:8080/
    Resource URIs

Installing and running the PHP based employee data demo (App Builder)

Use the following steps to install and run the application with App Builder:

  1. Open the App Builder according to the instructions in the Getting Started guide.
  2. Click Create from repository.
  3. Specify the module to copy: zero:zero.phpemployee.demo.
  4. Application name: phpemployee.demo
  5. Destination folder: directory into which the new application folder is created, or keep in default directory.
  6. Click "Create"; the phpemployee.demo application is shown in your application list.
  7. Click "Edit" icon to open the phpemployee.demo application
  8. Switch to "Console" tab for creating the database artifacts by running the following commands
    zero runsql setup_db.sql
  9. Click the Start button to start this application.
  10. After following the previous steps, you can interact with the application in the following ways:
    Dojo-based UI
    http://localhost:8080/
    Resource URIs

Overview of the application

Resource handling

The employee data demo is a RESTful application, and makes use of WebSphere sMash's simple convention for exposing resources in a RESTful manner. In this case, the set of employees is the collection and each individual employee is a resource. The application needs to be able to get the set of employees and add to it and to be able to edit and delete individual employees. The employees collection is represented as a file named employees.php under the app/resources folder. It contains a class called Employees, which contains methods for getting the set of employees (onList), getting a single employee details (onRetrieve), adding new employees (onCreate), updating an existing employee (onUpdate) and deleting an employee (onDelete). The existence of this file causes WebSphere sMash to automatically route requests for http://<host>/employees http://<host>/employees/<employeeId> to the employees.php script.

PHP scripting

Within the employees.php script are the Employee class methods onList, onRetrieve, onCreate, onUpdate, and onDelete. The HTTP method used by the incoming request and whether the URI contains an employee id or not determines which method in the script is called. If a GET request is made for http://<host>/employees, for example, the onList will be called and return the list of employees in JSON format.

Data access

When a request comes in to access or manipulate employee data the application needs to talk to a backend database. This is done through the WebSphere sMash Data APIs. After setting up the database configuration information in the config/zero.config, the create call sets up a new instance of the PHP data manager. Queries and updates to the database can then be done through the WebSphere sMash Data APIs.

JSON APIs and rendering

WebSphere sMash provides support for serialization and deserialization of JSON objects. The employee data demo leverages this support both for reading in employee information and sending it back to the client. Since JSON is sent by the Dojo client whenever an employee is created or updated, the employee data demo uses the json_decode call using the PHP superglobal $HTTP_RAW_POST_DATA to pull out the information from the request and insert it into the database call. For rendering JSON back to the client, the PHP object representing the employee(s) (returned by the WebSphere sMash Data APIs) is encoded using the function call json_encode.

Client programming with Dojo

The employee data demo uses Dojo for its client interface. The index.html file in the public folder contains the HTML and Javascript used by the client.

The employee data demo client interface: screenshot.png

Version 1.1.31300