【问题标题】:Default routing to route to /directory/index.php Google App Engine默认路由路由到 /directory/index.php Google App Engine
【发布时间】:2019-12-03 23:50:07
【问题描述】:

我有一个问题,我在这个网站上看到过很多讨论,但我没有看到它讨论过运行 php72 或 php73 运行时的应用程序(如果有,我还没有看到它解决)。

我正在尝试获得最简单的 php Web 服务器的基本功能,当您导航到一个目录时,它可以为该目录中的 index.php 提供服务。

很明显,Google App Engine 并没有开箱即用,但我必须相信有办法获得该功能。

我看了这个问题,看起来它有我需要的东西:
Google app engine redirect everything to index.php

问题是,如果您在 php72 或 php73 运行时中,这不起作用,我需要这样做。 app.yaml 文件中 script 名称的唯一可接受值是 auto。大多数其他问题都有相同类型的答案(因为它们告诉您重新定义脚本值,但在我的运行时这不起作用)。

这是我当前的代码...我正在正确地提供我的所有图像、js 和 css:

runtime: php73

handlers:

- url: /assets
  static_dir: assets

# Serve static files as static resources.
- url: /(.+\.(gif|png|jpg|svg|webp|jpeg|js))$
  static_files: \1
  upload: .+\.(gif|png|jpg|svg|webp|jpeg|js)$

- url: /style
  static_dir: style

- url: /js
  static_dir: js

- url: /.*
  script: auto

这是我的目录结构:

根目录下的 index.php 完美地服务于一切,当我尝试导航到 /forgot//setup-account/ 目录时,它就不起作用了。当我说不起作用时,我的意思是根 index.php 提供的内容与 /forgot//setup-account/ 目录中显示的内容相同。

如何从/forgot//setup-account/ 目录提供index.php 文件?

【问题讨论】:

  • 这个想法是你的根 index.php 处理你的应用程序的所有入站请求。因此,我建议您在根 index.php 的顶部添加一行,用于执行您正在寻找的路由。如果这是可以接受的并且您需要帮助,请告诉我们。

标签: php google-app-engine google-cloud-platform


【解决方案1】:

所以我将尝试尽可能深入地回答这个问题,因为这是我一直在寻找的,而我在其他任何地方都没有找到。

FIRST - 这是使用 php72 或 php73 运行时,在该运行时之前的任何内容在 SO 和其他网站上都有其他答案,以及这两个运行时之后的任何内容,我只是不确定。

另外,让我们定义一下我的问题:

我的问题是,当我导航到我的 PHP Web 应用程序中的子文件夹(我的 index.php 文件包含我的控制器代码)时,唯一会弹出的内容是我的应用程序根目录中的内容(以前称为“/”)。因此,当我导航到“/”时,所有内容都被正确渲染和显示,但每当我离开那里时,它只会继续显示相同的内容。

我以前(现在仍然)对 Laravel、Slim 或 Symfony 等框架不感兴趣,所以我必须弄清楚如何在没有框架的情况下做到这一点,这被证明具有挑战性,因为它看起来像所有在线教程只处理框架。所以,下面是我的普通 PHP,不到 20 行代码就可以回答如何做到这一点。

1. 在您的 app.yaml 中,将入口点路径添加到将成为您的“路由器”的脚本。我的看起来像这样(查看 cmets 以获得简要说明):

runtime: php73

# I'm choosing to serve my router from the root of my application, and call it router.php. 
# You can serve it from wherever, and call it whatever. 
# So, for instance, you this line could read: serve /model/folder/path/whatever.php 
entrypoint: serve /router.php

handlers:
  # my handlers for css, js, and images

2. 现在,在我的router.php 中,这是让魔法发生的代码:

<?php

require_once get_required_php_file(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

function get_required_php_file(string $path): string {

    // just require the index file
    if ($path === '/') {
        return 'index.php';
    }

    // getting the last character to strip it off if it's a "/"
    $last_char = \substr($path, -1);
    $pathname = ($last_char === '/' ? rtrim($path, '/') : $path);

    // stripping the leading "/" to get the true path
    $pathname = ltrim($pathname, '/');

    // setting the full require path
    $full_php_path = $pathname.'/index.php';

    // returning the path as long as it exists, if it doesn't, return 404.php
    return (\file_exists($full_php_path) ? $full_php_path : '404.php');

}

请参阅 cmets 以了解此处发生的情况......尽管它应该是非常自我解释的。我从用户那里获取 request_uri,只是将 index.php 附加到路径名上并要求它。

所以,就是这样。 12 行代码的廉价普通路由器(如果您消除了空格和 cmets)。

发现问题 1. 如果您像我一样查看包含如下页眉和/或页脚的文件:

// view.php
<?php
declare(strict_types=1);

include_once '../view/header.php';
include_once '../view/footer.php';

您需要检查并更改包含路径以包含 您的路由器文件所在的位置。对于我的示例,这很简单,因为我将路由器放在了根目录。因此,如果我想更新上面不正确的视图示例以正确包含文件,上面的代码现在看起来像这样:

// view.php
<?php
declare(strict_types=1);

include_once 'view/header.php';
include_once 'view/footer.php';

【讨论】:

【解决方案2】:

这太棒了!谢谢。

我需要做一点小调整才能让它对我有用。 我为页面提供了每个暴露的 php 文件的名称。 像这样: myurl.com\formfrontend.php or myurl.com\mygmailservice.php

所以我需要换行: $full_php_path = \ltrim(\rtrim($path, '/'), '/').'/index.php'; 对此: $full_php_path = \ltrim(\rtrim($path, '/'), '/');

【讨论】:

    猜你喜欢
    • 2012-11-15
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多