默认路由到/directory/index.php谷歌应用程序引擎
我在这个网站上看到过很多关于这个问题的讨论,但是对于运行php72或php73运行时的应用程序,我还没有看到讨论过这个问题(如果有,我还没有看到它得到解决) 我试图获得最简单的php web服务器的基本功能,当您导航到一个目录时,它可以服务于该目录中的index.php 显然,谷歌应用程序引擎并没有做到这一点,但我必须相信,有一种方法可以实现这一功能 我看了这个问题,它似乎有我需要的:默认路由到/directory/index.php谷歌应用程序引擎,php,google-app-engine,google-cloud-platform,Php,Google App Engine,Google Cloud Platform,我在这个网站上看到过很多关于这个问题的讨论,但是对于运行php72或php73运行时的应用程序,我还没有看到讨论过这个问题(如果有,我还没有看到它得到解决) 我试图获得最简单的php web服务器的基本功能,当您导航到一个目录时,它可以服务于该目录中的index.php 显然,谷歌应用程序引擎并没有做到这一点,但我必须相信,有一种方法可以实现这一功能 我看了这个问题,它似乎有我需要的: 问题是,如果您在php72或php73运行时,这就不起作用了,我需要这样做。app.yaml文件中脚本名称的
问题是,如果您在php72或php73运行时,这就不起作用了,我需要这样做。app.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
这是我的目录结构:根目录下的index.php完美地服务于一切,当我尝试导航到
/forget/
或/setup account/
目录时,它就不起作用了。当我说不起作用时,我的意思是root index.php提供的内容与/forget/
或/setup account/
目录中显示的内容相同
如何提供
/forget/
和/setup account/
目录中的index.php文件?因此我将尝试尽可能深入地回答这个问题,因为这是我在搜索的内容,而我在其他任何地方都找不到
首先-这是使用php72或php73运行时,运行时之前的任何内容,SO和其他站点上的其他答案,以及这两个运行时之后的任何内容,我只是不确定
另外,让我们来定义我的问题:
我的问题是,当我导航到PHP web应用程序中的子文件夹(其中包含控制器代码的index.PHP文件)时,唯一会弹出的是应用程序根目录下的内容(迄今为止称为“/”)。因此,当我导航到“/”时,所有内容都被正确地呈现和显示,但每当我离开那里时,它都会继续显示相同的内容
我对Laravel、Slim或Symfony这样的框架不感兴趣,所以我必须想办法在没有框架的情况下做到这一点,这很有挑战性,因为看起来所有在线教程都只涉及框架。所以,下面是我的香草PHP,不到20行代码回答如何做到这一点
1。在app.yaml中,向脚本添加一个入口点路径,该路径将成为您的“路由器”。我的是这样的(查看评论中的简短解释):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');
}
这太棒了!谢谢 我需要一个小的调整,使它为我工作。 我提供的页面上显示了每个php文件的名称。 这样地:
myurl.com\formfrontend.php或myurl.com\mygmailservice.php
所以我需要改变路线:
$full\u php\u path=\ltrim(\rtrim($path,'/'),'/')。/index.php'代码>
为此:
$full\u php\u path=\ltrim(\rtrim($path,'/'),'/')代码>我们的想法是,root index.php处理应用程序的所有入站请求。因此,我建议您在root index.php的顶部添加一行,以完成您正在寻找的路由。如果这是可以接受的,并且您需要帮助,请告诉我们。我已经创建了一个公共要点,其中存储了最新的路由器代码和我的app.yaml文件:谢谢,这很有效。非常棒的添加,谢谢。
<?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');
}
// 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';