在 Google App Engine Standard 上运行 php 网站不会路由到子文件夹和文件

问题描述 投票:0回答:1

我正在尝试将我们的网站从 Linux 虚拟机(也在 google 上)迁移到 google App Engine 标准环境。

当我部署应用程序并测试它时,主页(index.php)工作正常,但是当我尝试转到其他文件时,例如 /somefolder/somefile.php ,它就不行了。它只显示index.php,但没有图片等

我在网上搜索了一下,发现这可能是因为没有前端控制器(?)

我的app.yaml文件如下:

service: nameoftheapp
runtime: php83

handlers:
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
  static_files: \1
  upload: .+\.(gif|png|jpg)$

- url: /(.+\.php)$
  script: auto

- url: /.*
  script: auto

我的index.php是:

<?php



// android store
if (preg_match('#android#i', $_SERVER ['HTTP_USER_AGENT'])) {
    header('Location: market://details?id=nl.myapp');
    exit;
}

// ios
if (preg_match('#(iPad|iPhone|iPod)#i', $_SERVER ['HTTP_USER_AGENT'])) {
    header('Location: https://apps.apple.com/us/app/myapp/id973246494');
    exit;
}
echo "<html> <head> </head> <body>";
echo '<center><p><br><p><br>Sometext</center> <p>';
echo '<center> some more text.</center> <p>';
echo "<center> <img width='800' src='images/logo_GW_forweb.jpg'</center> <p>";
echo "<center> yet some more text</center> <p>";
echo "</body>";

?>

index.php 作为一个简单的登陆页面,供用户将其重定向到应用程序的应用程序商店。就目前而言,这很有效。还显示位于子文件夹中的徽标。

但我自己想去https://mywebsite.nl/somefolder/somefile.php

这部分不起作用。是否可以通过设置正确的 app.yaml 来解决这个问题(我有大约 10 个子文件夹,其中一些子文件夹有自己的子文件夹,总共有 100 多个 .php 文件)

我还需要别的东西吗?我希望 app.yaml 有一个设置,可以将所有请求路由到正确的位置。

我制作了一个测试应用程序来看看如何让它工作。这个可行,但我怀疑这就是正确的方法。

app.yaml 文件指出:

runtime: php83
service: test

handlers:
- url: /.*
  script: index.php

索引指出:

<?php
$requestUri = $_SERVER['REQUEST_URI'];

// Hier kun je logica toevoegen om het verzoek te routeren naar de juiste actie/controller
if ($requestUri === '/test/test.php') {
    require 'test/test.php';
} elseif ($requestUri === '/root.php') {
    require 'root.php';
} else {
    // Standaard HTML-tekst als de URI niet overeenkomt met specifieke routes
    echo "<!DOCTYPE html>";
    echo "<html lang=\"en\">";
    echo "<head>";
    echo "    <meta charset=\"UTF-8\">";
    echo "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">";
    echo "    <title>Test App</title>";
    echo "</head>";
    echo "<body>";
    echo "    <h1>Hallo dit is een test</h1>";
    echo "    <p>Welkom bij de PHP-testapplicatie op Google App Engine!</p>";
    echo "    <p><a href=\"test/test.php\">Ga naar test.php</a></p>";
    echo "    <p><a href=\"root.php\">Ga naar root.php</a></p>";
    echo "</body>";
    echo "</html>";
}
?>

这个有效。我可以访问 root.php 以及位于子文件夹 test 中的 test.php 。但我怀疑这是否适合我自己的网站。

php google-app-engine app.yaml front-controller
1个回答
0
投票
  1. 你是对的,你需要一个前端控制器(ref)。 Google 建议使用 Laravel、Symfony 或 Slim 等框架。

  2. 如果您更愿意使用

    index.php
    文件(而不是框架)并且您还有很多子路径(子文件夹),那么您应该编写前端控制器代码以使用正则表达式来检测路由。

    是来自Google的示例,用例与您的类似 - 用户登陆主页(

    index.php
    ),其中包含将用户带到网站其他部分的链接

© www.soinside.com 2019 - 2024. All rights reserved.