通过入口点标准使用前端控制器进行php72动态网址路由不起作用,Google App Engine [GAE]

问题描述

我在GAE的网站上没有读取我的动态URL,因为我想使用传入的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以及about.PHP和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是我期望$ _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>

我测试了它,现在可以正常使用了。