In the previous article, Selecting a Cloud Technology, we settled on using Google’s App Engine for this project. In this article, we will look at setting up the App Engine SDK for PHP, and discuss code editors and source control for this project.

 

Google App Engine SDK for PHP

Google provides a Software Development Kit (SDK) for PHP, which is the language we will use to code the majority of our application. The SDK can be downloaded here. There are also instructions on how to install the SDK once downloaded. The SDK gives us access to a number of features that will assist us with developing and testing our application locally. By using the SDK, we will be able to emulate the live App Engine Environment, including the configuration from our app.yaml file (to be discussed later). We will also have access to a local version of DataStore, which we will use to store data, as well as an emulation of memcached which we will use to speed up access to data. The SDK also includes Google App Engine Launcher, which allows us to launch our application locally, and deploy our application to our live App Engine account. The launcher also provides a log feature, which I have found essential for debugging App Engine applications.

 

Code Editor

Whilst you do not require a dedicated code editor to develop in PHP, I would highly recommend finding a code editor that suits the way you work. Code editors can assist you with code highlighting, code folding, indexing, live previews, SSH access, Source Control via Git and much more. I personally use Coda 2, a code editor produced by Panic Inc. Coda is a Mac only product, that has many useful features. More details can be found at Panic Inc. Coda Has in-built Git support, which we will utilise to maintain our source code and version control. I found a list of other good code editors at Elegant Themes.

 

Getting Started

Once you have installed the GAE SDK, and have selected a code editor, we can begin with a simple “Hello World” application to ensure we have everything set up correctly. Let’s create a simple helloworld.php file.

 

As you can see there is nothing to special about our Hello World application. We simply set a message, and echo it to the screen. If you were to run this helloworld.php file in a typical PHP development environment, it would display Hello World! and the application would be working correctly. Google App Engine however, requires some further information to run correctly.

The app.yaml File

Google App Engine requires each project, and later each module to have a .yaml file that tells App Engine how the application should be run. Let’s create a simple app.yaml file, which is the default .yaml file for the application.

Let’s have a look at what we have put in the app.yaml file. First we define our application environment variables:

  1. application: buzilog – When you sign up for Google App Engine, and set up an app, you are given an application id. We need to provide this in our app.yaml file to identify our application.
  2. version: dev-001 – We need to set a version number so that we can manage versions of our application. In this case I have set dev-001, to indicate we are in development, and this is version 1.
  3. runtime: php55 – App Engine needs to know which language to expect, and which version it should run. At this stage App Engine’s most current version is php55.
  4. api_version: 1 – The API version allows you to specify which version of the Google Ap Engine API’s you are using. The current version number is 1. This allows your application to continue working on the version 1 API, should google upgrade to version 2, allowing you to test for compatibility problems before you are forced to switch.
  5. builtins: – remote_api: on – This allows us to access Google Cloud Services that are not included in the App Engine API’s.

Next, we need to define some handlers to tell App Engine how to handle requests.

handlers:
– url: /*.*
script: helloworld.php

We have set a handler, to intercept requests to all files, ie any filename, with any filetype. We have then asked App Engine to run the helloworld.php script when it intercepts the request. This will mean that anytime we try to access our application, our helloworld.php script will be run. To add our application to the SDK Launcher, open the GAE Launcher, and select the + button and navigate to your app.yaml file. It should then appear as shown below.

 

You can now select the application, and click the “Run” button, and the application will be started on your local machine. You will also see a “Port” number in the far right column, in my example the port is 13080. If you visit http://localhost:13080 (substituting your port number), you should now see our Hello World! message! There is also a browse button in the launcher that will launch the app for you in your browser. As we set our app.yaml to intercept all requests and serve our helloworld.php script, we should be able to access out message from any url! try http://localhost:13080/anotherpage.php and you will see we still get the same page! We will talk more about configuring using app.yaml later, but at this stage you should have a working helloworld.php app serving from any url on your localhost port.

In the next article, we will have a look at a development approach known as Test Based Development, and we will set up our development environment to allow us to use a test based development approach.