Join Joseph Lowery for an in-depth discussion in this video Coding your app, part of Google App Engine Essential Training.
- View Offline
- Time to start coding. Make sure you set up the App Engine SDK for PHP as outlined in the previous lesson so that you install the Google App Engine launcher. Now to demonstrate the first app, we're gonna need to create just two files. One for configuring and the other to run the PHP code. So the first step is to create a folder to hold our apps. I'm going to put this right on the Desktop. And I'll call it ae-php.
We'll use that folder to contain all of our example apps. So, let me create another folder within it. And I'll call this one test-ae-01. All right, I'm using Aptana as my code editor. Then I'm going to set up a new project within it. And easiest way to do that is to expand the Local File System Desktop. And then choose ae-php folder.
Click Promote to Project. Now I can drill down to there into my test-ae-01 folder. All right, I'm ready to create my first file. I'll go to File, New, File. And in the dialog, I'll enter my file name, main.php. I can see that it's gonna be stored in the test-ae-01 folder. That's perfect. Click Finish. And now, when the file opens, I'm gonna add some basic PHP code to start it out.
This is a echo statement and I'll put in a little h1 tag. And then close that h1 off. And also close off the php. Naturally, you could put whatever PHP code you want in this page but this will be enough for us to confirm that everything is up and running. All right, let's make the second of our two files. Again, I'll go to File, New, File. This one is called app.yamil.
Now let's code the file and then I'll break it down line by line as we go. First, we're gonna name the application. application:, let's just call it firstapp. Now, if you haven't worked with Yamil before, you'll see that it's basically a series of name-value pairs using a colon as a separator without quote. So next, we wanna give the version of the app and that's gonna be version: 1.
And then we specify the runtime. And we're gonna be using php as language for our runtime, and its current api_version which is 1. Now that's the core configuration. We just need to set up a request handler now to point to our main PHP file. So we'll enter the keyword handlers, followed by a colon. And then on the next line, make sure we just have one space there and a hyphen which indicates a list of name-value pairs in the ML.
And the first of those which is gonna be url:, space, and then forward slash, and a wildcard series, dot, asterisk. This is a regular expression that basically means every request at the root of the app. And once that request is received, the next line will indicate what to do. And we want that to be script: main.php. So what this is saying is when the request for anything in this site root occurs, run the script: main.php.
All right, so let's save everything. And now let's head back to the App Engine launcher. And first, I'll set up a project. Since we already have the files created, I'm gonna choose File, Add Existing Application. And in the dialog, I'll click Browse to navigate to my Desktop. ae-php, a little too quick there, test-ae-01. And click Choose.
Now note that both the Admin Port and Port values are supplied for you. You can, of course, change those but let's leave them at their default values for now. And I'm ready to click Add. And now our app is listed. You can see that the launcher read the app.yamil file to pick up the name of the application, firstapp. And the Path and Ports are also listed. So since it's already selected, let's run it. The little green dot that you see indicates that it was ran successfully.
Now to view the app, we're gonna need to, of course, look at it in the browser. And you can do that either by switching to a browser and entering in the address manually, http://localhost:8080 which would be the Port or you can just click Browse. And there's my echoed PHP code. Once the app has been launched, we can make any changes we like directly in the PHP file. So let's go back to the code editor, and our main.php file.
And I'll add another echo line here. Let's make this an h2 tag. And then close it off. I'll save the file. And now let's go back to the browser. I'll refresh. Looks like I've got a little error. Let's go back. And I forgot the semicolon, of course. We'll save that. No errors indicated, and back. Refresh one more time. And now, there's my new code. Great, so our SDK is officially up and running properly.
- Understanding the Google App Engine
- Using the Google Cloud Console
- Setting App Engine services
- Coding your app
- Working with images, style sheets, and other static files
- Incorporating HTML templates
- Uploading and deploying your app
- Implementing Google Cloud Storage
- Setting up a custom domain