默认路由以路由到/directory/index.php Google App Engine [英] Default routing to route to /directory/index.php Google App Engine

查看:72
本文介绍了默认路由以路由到/directory/index.php Google App Engine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我已经在这个站点上谈论了很多,但是我还没有看到它针对运行php72或php73运行时的应用程序谈论过(如果已经解决了,我还没有看到它解决了) ).

I have a problem that I've seen talked about a lot on this site, but I haven't seen it talked about for applications running the php72 or php73 runtime (and if it has, I haven't seen it resolved).

我正在尝试获得最简单的php Web服务器的基本功能,即当您导航到目录时,它可以为该目录中的index.php提供服务.

I'm trying to get the basic functionality of most straightforward, php web servers, that when you navigate to a directory, it can serve the index.php that lives at that directory.

显然,Google App Engine并没有做到这一点,但我必须相信有一种方法可以获取该功能.

Obviously out of the box, Google App Engine doesn't do that, but I have to believe that there is a way to get that functionality.

我看着这个问题,看起来像它有我所需要的:
Google App引擎将所有内容重定向到index.php

I looked at this question, which looked like it had what I needed:
Google app engine redirect everything to index.php

问题是,如果您在php72或php73运行时中,那是行不通的,而我需要这样做. app.yaml文件中script名称的唯一可接受值是auto.大多数其他所有问题都有相同的答案类型(因为它们告诉您重新定义脚本值,但是在我的运行时中这是行不通的).

The problem is, that doesn't work if you're in the php72 or php73 runtime, which I need to be. The only acceptable value for the script name in the app.yaml file is auto. Most all other questions have the same type of answer (in that they tell you to redefine the script value, but in my runtime that just doesn't work).

这是我当前的代码...我正确地提供了所有图像,js和css:

Here is my current code...I'm serving all of my images, js, and css correctly:

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

这是我的目录结构:

Here is my directory structure:

位于根目录的index.php可以完美地提供所有服务,这是在我尝试导航到/forgot//setup-account/目录时的情况,当它不起作用时.当我说行不通时,是指/forgot//setup-account/目录中显示的内容与根index.php所提供的内容相同.

The index.php at the root is serving everything perfectly, it's when I try and navigate to the /forgot/ or /setup-account/ directories is when it doesn't work. And when I say doesn't work, I mean that the same content that the root index.php serves is what is shown at the /forgot/ or /setup-account/ directories.

如何为/forgot//setup-account/目录中的index.php文件提供服务?

How do I serve the index.php files from the /forgot/ and /setup-account/ directories?

推荐答案

因此,我将尽我所能尽可能深入地回答这个问题,因为这是我一直在寻找的,并不是我真正想要的找到其他地方.

So I'm going to try and make this answer as in-depth as I can, because this was what I was searching for that I didn't really find anywhere else.

第一-这是使用php72或php73运行时,在该运行时之前的所有操作在SO和其他站点上还有其他答案,而在这两个运行时之后的操作都不确定,我只是不确定.

FIRST - This is using either the php72 or php73 runtime, anything before that runtime there are other answers on SO and other sites, and anything after these two runtimes, I'm just not sure.

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

Also, let's define my problem:

我的问题是,当我导航到PHP Web应用程序中的子文件夹(其中index.php文件包含控制器代码)时,唯一会弹出的内容是应用程序根目录下的内容. (以前称为"/").因此,当我导航到"/"时,所有内容都可以正确渲染和显示,但是无论何时我离开那里,它都将继续显示相同的内容.

My problem was that when I would navigate to subfolders in my PHP web app (where my index.php files were that contained my controller code), the only thing that would pop up would be the content that was at my application's root (heretofore known as "/"). So, everything was being rendered and displayed correctly when I would navigate to "/", but anytime I navigated away from there, it would just keep showing the same content.

我对Laravel,Slim或Symfony这样的框架一无所获,所以我不得不弄清楚如何在没有框架的情况下做到这一点,这被证明是充满挑战的,因为它看起来像在线上的所有教程一样只处理框架.因此,下面是我的原始PHP,不到20行的代码回答了如何做到这一点.

I had (and still have) no interest in a framework like Laravel, Slim, or Symfony, so I had to figure out how to do this without a framework, which proved challenging, because it looked like all of the tutorials online only dealt with frameworks. So, below is my vanilla PHP, less than 20 lines of code answer to how to do this.

1..在您的app.yaml中,向脚本添加入口点路径,该路径将成为您的路由器".我的看起来像这样(请在评论中查看简要说明):

1. In your app.yaml, add an entrypoint path to the script that will be your "router". Mine looks like this (look in the comments for a brief explanation):

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中,这是使魔术发生的代码:

2. Now, in my router.php this is the code that makes the magic happen:

<?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附加到路径名上并要求它.

Please see the comments for an explanation on what's happening here...although it should be pretty self explanatory. I'm taking the request_uri from the user and just appending index.php onto the pathname and requiring it.

就是这样.一种便宜的,带有12行代码的香草路由器(如果您消除了空白和注释).

So, that's that. A cheap, vanilla router in 12 lines of code (if you eliminate the whitespace and comments).

GOTCHAS 1.如果像我一样,您具有包含如下页眉和/或页脚的查看文件:

GOTCHAS 1. If, like me, you have view files that include headers and/or footers like this:

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

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

您需要检查并更改包含路径,以从路由器文件所在的包含.对于我的示例,这很容易,因为我将路由器置于根目录.因此,如果我想更新以上不正确的视图示例以正确包含文件,则上面的代码现在将如下所示:

You need to go through and change your include paths to include from where your router file is. For my example, it's easy, because I put my router at the root. So if I wanted to update the above incorrect view example to correctly include the files, that above code would now look like this:

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

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

这篇关于默认路由以路由到/directory/index.php Google App Engine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆