【问题标题】:How to construct a app.yaml file?如何构造一个 app.yaml 文件?
【发布时间】:2013-07-11 08:03:30
【问题描述】:

我正在为 php 试用 GAE,但在 app.yaml 文件构建中迷失了方向。我可以理解谷歌教程中显示如何将所有 url 请求指向单个文件的部分

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

但这对我没有帮助。我将发布我设置的内容,文件结构在图片中。

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

所以我的 xxx.appspot.com 或 xxx.appsport.com/main 的登录页面将是 main.php。而且效果很好。

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');

因此它加载启动会话并获取用户名和电子邮件进行一些字符串格式化,然后设置为会话变量,然后我检查以匹配条件并根据条件重定向到另一个脚本,该脚本进行更多验证和很快。制作很简单,我只是将其重定向到 login.php

登录.php

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

所以显示的输出应该是会话变量名,但我得到了这个

那么我做错了什么?我将使用登录页面从 SQL 数据库中提取用户数据,并根据值将用户重定向到不同的页面,这些页面将根据其设置显示不同的表单、表格和报告。

例如。来自 login.php

如果 userA 属于 Dept1

header('位置:/Dept1/main.php');

否则

header('位置:/Deptx/main.php');

所以我预计会有很多重定向,并且每个重定向都必须能够继承设置的会话变量。在普通 PHP 服务器上运行时,我能够做到这一点。 GAE 版本需要一些重新学习。我要提前感谢任何人花时间阅读直到听到。谢谢。

如果有人能就如何使用 app.yaml 以及如何将它与 w3school 中的演示示例一起使用做详细教程,那就太好了。

【问题讨论】:

    标签: php google-app-engine redirect app.yaml


    【解决方案1】:

    您的 app.yaml 看起来不错;它只是不完整的。

    您已经定义了 //main 映射到您的 main.php 脚本,并且工作正常。

    但是当用户的浏览器请求/login.php时,App Engine在app.yaml中查找并没有找到任何匹配的路由,所以你会得到这个404错误。

    要处理这种特殊情况,您可以使用 url: /login.phpscript: login.php 输入另一个条目。

    然后我会查看您的申请并确保您没有遗漏任何其他路线。

    您可能还需要在 app.yaml 中的 URL 中使用通配符。否则,如果您的应用程序曾经将用户发送到像 /main/subpage 这样的 URL,它不会转到 main.php 处理程序,因为它不匹配 app.yaml 中的任何路由。在这种情况下,您可能希望使用url: /main.\* 作为示例。或者,您可以在 app.yaml 末尾使用包罗万象的 /.* 处理程序。

    您可以在 PHP app.yaml 参考页面上了解这些通配符和其他 app.yaml 选项: https://developers.google.com/appengine/docs/php/config/appconfig

    (不过,您的样式表、javascript 和图像不需要通配符,因为您已经为它们使用了 static_dir。)

    【讨论】:

    • 我假设 App Engine 在匹配 URL 时只考虑路径名(忽略查询字符串),但我可能错了。
    • 感谢您的检查。我会试试这个。你能解释一下特殊字符是如何工作的吗?
    • 特殊字符允许您使用正则表达式,这是一种在字符串中查找模式的广泛技术。他们值得学习;除了你的 app.yaml,你可能希望将来在你的 PHP 中使用它们。这里是a tutorial,这里是a PHP-specific guide
    • 感谢@JamieNiemasik
    【解决方案2】:

    这是我的,它可能会有所帮助。 Pcode 只是我的文件夹之一(不确定您是否需要定义文件夹,但我还是把它留在那里):

    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
    

    基本上,我认为以下行的运行类似于hostgatorgodaddy 等常规 php 主机:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 2013-07-10
      • 2018-06-29
      • 2016-05-07
      • 2017-04-15
      相关资源
      最近更新 更多