6

I am trying out GAE for php and got lost in the app.yaml file construction. I can understand the part from the google tutorial which shows how to point all url request to a single file

https://developers.google.com/appengine/docs/php/gettingstarted/helloworld

But it does not help in my case. I am going to post what I have setup and the file structure is in the pic.enter image description here

App.yaml

application: xxx
version: 1
runtime: php
api_version: 1
threadsafe: true

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /images
  static_dir: images

- url: /scripts
  static_dir: scripts

- url: /favicon.ico
  static_files: favicon.ico
  upload: favicon.ico

- url: /
  script: main.php
  login: required
  auth_fail_action: redirect

- url: /main
  script: main.php
  login: required
  auth_fail_action: redirect

So my landing page for xxx.appspot.com or xxx.appsport.com/main would be main.php. And it works fine.

main.php

<?php
session_start();
date_default_timezone_set('America/Los_Angeles');
require_once 'google/appengine/api/users/UserService.php';

use google\appengine\api\users\User;
use google\appengine\api\users\UserService;

$user = UserService::getCurrentUser();
$name= $user->getNickname();
$name = explode(".",$name);
$name[0]= ucfirst($name[0]);
$name[1]= ucfirst($name[1]);
$name = $name[0]." ".$name[1];
$_SESSION['name']=$name;
$_SESSION['email']= getenv('USER_EMAIL');

header('Location: login.php');

So it loads starts a session and gets the user name and email do a few string formatting and then set as Session variable and then I check to match a condition and based on the condition redirect to another script which does some more validation and so on. To make is simple I am just redirecting it to login.php

login.php

<?php
session_start();
echo $_SESSION['name'];

So the out put displayed should be the session variable name but instead I get this

enter image description here

So what did I do wrong? I am going to use the login page to pull user data from a SQL db and based on the value redirect the user to different pages which will display different forms, tables, reports based on their settings.

Eg. From login.php

if userA belongs to Dept1

header('Location: /Dept1/main.php');

Else

header('Location: /Deptx/main.php');

So I expect a lot of redirects and each of the redirects must be able to also carry over the session variables that are set. I as able to do this while running on normal PHP server. The GAE version requires some re learning. I would like to thank any one in advance just for taking time to read till hear. Thank you.

Also it would be nice if some one could do a detail tutorial on how to use app.yaml and how it could be used with a demo example like in w3school.

2 Answers2

10

Your app.yaml is looking good; it's just incomplete.

You've defined that / and /main map to your main.php script, and that works fine.

But when the user's browser requests /login.php, App Engine looks in app.yaml and does not find any route that matches, so you get this 404 error.

To handle this particular case, you can another entry with url: /login.php and script: login.php.

Then I would look through your application and make sure you're not missing any other routes.

You also may need to use wildcards in your URLs in app.yaml. Otherwise, if your application ever sends the user to a URL like /main/subpage, it will not go to the main.php handler because it matches no routes in app.yaml. In this case you might want to use url: /main.\*, as an example. Or you can use a catchall /.* handler at the end of your app.yaml.

You can learn about these wildcards and the other app.yaml options on the PHP app.yaml reference page: https://developers.google.com/appengine/docs/php/config/appconfig

(You don't need wildcards for your stylesheets, javascript, and images, though, because you've used static_dir for those.)

Rich
  • 3,094
  • 1
  • 23
  • 52
Jamie Niemasik
  • 770
  • 6
  • 16
  • I'd assume App Engine only considers the path name (ignoring the query string) when matching URLs, but I could be wrong. – icktoofay Jul 14 '13 at 01:16
  • Thanks for checking. I will try this out. Can you also explain how the special characters work? – Abilash Amarasekaran Jul 16 '13 at 13:57
  • The special characters allow you to use regular expressions, a widespread technique for finding patterns within strings. They're worth learning; besides your app.yaml, you may wish to use them in your PHP in the future. Here's [a tutorial](http://regexone.com), and here's [a PHP-specific guide](http://www.noupe.com/php/php-regular-expressions.html). – Jamie Niemasik Jul 16 '13 at 14:25
  • Thanks @JamieNiemasik – Abilash Amarasekaran Jul 20 '13 at 10:31
3

Here's mine, it may help. Pcode is just one of my folders (not sure if you need to define folders but I just left it there anyway):

application: theclearview1
version: 10
runtime: php
api_version: 1

handlers:
- url: /(.*\.(htm$|html$|css$|js$))
  static_files: \1
  upload: (.*\.(htm$|html$|css$|js$))
  application_readable: true

- url: /css
  static_dir: css

- url: /js
  static_dir: js

- url: /(.*\.(ico$|jpg$|png$|gif$))
  static_files: \1
  upload: (.*\.(ico$|jpg$|png$|gif$))
  application_readable: true

- url: /Pcode/(.+)
  script: Pcode/\1

- url: /(.+)
  script: \1

- url: /.*
  script: index.php

Basically, I think the following lines will run similar to a regular php host like hostgator, godaddy etc.:

handlers:
- url: /(.*\.(htm$|html$|css$|js$))
  static_files: \1

  upload: (.*\.(htm$|html$|css$|js$))
  application_readable: true

- url: /(.*\.(ico$|jpg$|png$|gif$))
  static_files: \1

  upload: (.*\.(ico$|jpg$|png$|gif$))
  application_readable: true

- url: /(.+)
  script: \1

- url: /.*
  script: index.php
FrogTheFrog
  • 1,409
  • 18
  • 31