php72 动态 url 通过入口点标准使用前端控制器的路由不工作 Google App Engine [GAE]

php72 dynamic url routing with front controller via entry point standard not working Google App Engine [GAE]

我的动态 URL 没有在我的 GAE 网站上读取,因为我想使用传入的 GET 来查询我的数据库。

就目前而言,Php72 不允许来自 app.yaml 的路由必须来自前端控制器,通过默认为 publichtml/index.php 或 index.php.

我把我的改成了worker.php。下面的 listing.php URL 是动态的,我的意思是 listing/random-dynamic-text-here 而 index.php 和 about.php 是静态页面。

注意:页面index.php and about.php and listing.php被调用并显示在浏览器上,但我无法获取$_GET["linkcheck"]的内容;在 listing.php。参考下文。

app.yaml

runtime: php72

runtime_config:
  document_root:
  
handlers:

- url: /.*
  script: auto
  secure: always
  redirect_http_response_code: 301

entrypoint:
  serve worker.php 

//入口点worker.php

<?php 
ini_set('allow_url_fopen',1);
switch (@parse_url($_SERVER['REQUEST_URI'])['path']){
  case '/':
      require 'index.php';
      break;
  case '/index':
      require 'index.php';
      break;
  case '/index.php':
      require 'index.php';
      break;
  case '/about':
      require 'about.php';
      break;
  case '/about.php':
      require 'about.php';
      break;
  case '/listing':
      require 'listing.php';
      break;
  case '/listing.php':
      require 'listing.php';
      break;
  case '/listing/':
      require 'listing.php/';
      break;
  case '/listing.php/':
      require 'listing.php/';
      break;
  default:
      http_response_code(404);
      header("Location: https://www.example.com/404");
      exit();
}
?>

index.php

<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
</body>
</html>

about.php

<html>
<head>
<title>About</title>
</head>
<body>
<h1>About</h1>
</body>
</html>

下面的 listing.php 是我期望的 file/page $_GET["linkcheck"];

listing.php

<html>
<head>
<title>Listing</title>
</head>
<body>
<h1><?php $cool = $_GET["linkcheck"]; echo $cool; ?></h1>
</body>
</html>

为此,您需要修改两个文件:

worker.php


<?php 
ini_set('allow_url_fopen',1);
$path = @parse_url($_SERVER['REQUEST_URI'])['path'];
switch ($path){
  case '/':
      require 'index.php';
      break;
  case '/index':
      require 'index.php';
      break;
  case '/index.php':
      require 'index.php';
      break;
  case '/about':
      require 'about.php';
      break;
  case '/about.php':
      require 'about.php';
      break;
  case '/listing':
      require 'listing.php';
      break;
  case '/listing.php':
      require 'listing.php';
      break;
  case '/listing/':
      require 'listing.php';
      break;
  case   (preg_match('/listing.*/', $path) ? true : false) :
      require 'listing.php';
      break;
   default:
      http_response_code(404);
      header("Location: https://www.example.com/404");
      exit();
}
?>

这里的区别是匹配以/listing开头的正则表达式,并将其发送到列表脚本。

第二次修改是在列表上有这一行:

<h1><?php $cool = $_GET["linkcheck"]; echo $cool; ?></h1>

替换为这个:

<h1><?php $cool = $_SERVER['REQUEST_URI']; echo $cool; ?></h1>

我测试了它,它现在可以正常工作了。