gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:12:56 26 4
gpt4 key购买 nike

我有一个问题,我在这个网站上看到过很多讨论,但我还没有看到它针对运行 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

这是我的目录结构:

enter image description here

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

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

最佳答案

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

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

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

我的问题是,当我导航到我的 PHP Web 应用程序中的子文件夹时(我的 index.php 文件包含我的 Controller 代码),唯一会弹出的是我应用程序根目录下的内容(以前称为“/”)。因此,当我导航到“/”时,一切都在正确呈现和显示,但无论何时我离开那里,它都会继续显示相同的内容。

我过去(现在仍然)对 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

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

}

请查看评论以了解此处发生的情况……尽管它应该是不言自明的。我从用户那里获取 request_uri 并将 index.php 附加到路径名上并要求它。

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

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

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

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

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

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

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

关于php - 默认路由到/directory/index.php Google App Engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57206392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com