【问题标题】:Should PHAR createDefaultStub differentiate between CLI and browser?PHAR createDefaultStub 是否应该区分 CLI 和浏览器?
【发布时间】:2016-03-21 18:04:35
【问题描述】:

我的问题是关于createDefaultStub$indexfile$webindexfile。我的理解是,如果请求来自 cli,$indexfile 将被提供,$webindexfile 将通过浏览器请求提供。

我从任何一个来源都得到了相同的响应(“后端”),我误解了这种行为吗?还是我的实现有误?

谢谢!

目录树:

PHAR
+--app
    +--backend
        +--index.php //prints 'backend'
    +--frontend
        +--index.php //prints 'frontend'
+--build //destination for PHAR
+--build.php
+--index.php

/build.php

$phar = new Phar("build/app.phar", 0, "app.phar");
$phar->buildFromDirectory("./app");
$phar->setStub(
$phar->createDefaultStub(
    "backend/index.php", "frontend/index.php"
)

);

/index.php

include('phar://./build/app.phar');

来自 PHP 手册:

createDefaultStub ([ string $indexfile [, string $webindexfile ]] )

【问题讨论】:

  • 如果您喜欢冒险,可以查看the contents of the stub。您应该能够使用它来确定哪个条件失败,以及为什么您的网络索引没有被提供。

标签: php phar


【解决方案1】:

我的原帖: 您是否已经尝试直接从浏览器运行您的 phar 文件? (如果您的网络服务器无法将您的 .phar 文件识别为 php 文件,您可以暂时将其重命名为 .php)。

你会发现它会起作用。如果您的网址是http://localhost/phar/build/app.phar,它将自动重定向到http://localhost/phar/build/app.phar/frontend/index.php。而且从cli来看,显然没有重定向,它会自动使用backend/index.php文件(这是你phar文件中的默认值)。

这种重定向行为不会发生在您自己的 index.php 中。 (这就是为什么您在前端也看到 backend/index.php 文件,这是默认设置)您必须自己构建重定向部分。

如果你自己构建它,你可以像这样引用你的后端/前端文件:

include('phar://./build/app.phar/frontend/index.php');

请注意,如果您将您的 phar 文件包含在另一个文件中,php 会将其视为库 phar 文件,而不是完整的应用程序 phar 文件 [1]。我认为他们没有考虑到您可能希望您的库在 cli 上具有其他行为,而不是来自网络浏览器。

[1]http://php.net/manual/en/phar.using.intro.php(第一段)

编辑:

更多调查表明您可能发现了一个错误。在 php 中运行 phar 有两种方法。一种是使用流包装器 phar(就像您正在做的那样),一种是使用 phar 文件中的代码,以防您没有 phar 流包装器。

如果您使用在没有 phar 流包装器的情况下运行的代码,它会按预期工作。

试试这个:

  • 将您的 phar 重命名为 .php
  • 使用这个 index.php:

代码:

stream_wrapper_unregister('phar');
include('./build/app.php');

您将看到它现在按预期工作(但速度较慢,因为您不再使用内置流包装器)。

【讨论】:

  • 我想我明白你的答案了,我稍后会尝试并在此回复。谢谢你现在
  • 所以..您是说使用includerequire 将始终呈现$indexFile,并且只有通过将phar 作为URL 访问它才会呈现$webIndexFile?跨度>
  • @Objective_d,这就是我所说的,是的。我又调查了一些。看起来你发现了一个错误......请参阅编辑。
猜你喜欢
  • 1970-01-01
  • 2013-11-01
  • 2018-12-06
  • 1970-01-01
  • 2020-05-15
  • 2018-09-26
  • 1970-01-01
  • 2018-01-09
  • 2015-06-07
相关资源
最近更新 更多