默认路由到 /directory/index.php Google App Engine

Ada*_*urk 2 php google-app-engine google-cloud-platform

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

我试图获得最直接的 php web 服务器的基本功能,当您导航到一个目录时,它可以为位于该目录中的 index.php 提供服务。

很明显,Google App Engine 并没有做到这一点,但我必须相信有一种方法可以获得该功能。

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

问题是,如果您在 php72 或 php73 运行时中,这将不起作用,我需要这样做。scriptapp.yaml 文件中名称的唯一可接受值是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
Run Code Online (Sandbox Code Playgroud)

这是我的目录结构:

在此处输入图片说明

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

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

Ada*_*urk 6

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

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

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

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

我曾经(现在仍然)对 Laravel、Slim 或 Symfony 之类的框架不感兴趣,所以我必须弄清楚如何在没有框架的情况下做到这一点,这证明具有挑战性,因为看起来所有在线教程都只涉及构架。所以,下面是我的普通 PHP,不到 20 行代码回答了如何做到这一点。

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

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
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

请参阅评论以了解这里发生的事情......尽管它应该是不言自明的。我从用户那里获取 request_uri 并将 index.php 附加到路径名并要求它。

所以,就是这样。一个 12 行代码的廉价普通路由器(如果你去掉空格和注释)。

陷阱 1. 如果像我一样,您的视图文件包含这样的页眉和/或页脚:

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

include_once '../view/header.php';
include_once '../view/footer.php';
Run Code Online (Sandbox Code Playgroud)

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

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

include_once 'view/header.php';
include_once 'view/footer.php';
Run Code Online (Sandbox Code Playgroud)