PHAR createDefaultStub 是否应该区分 CLI 和浏览器?
Should PHAR createDefaultStub differentiate between CLI and browser?
我的问题是关于 createDefaultStub
的 $indexfile
和 $webindexfile
。我的理解是,如果请求来自 cli,将提供 $indexfile
,而浏览器请求将提供 $webindexfile
。
我从任一来源得到相同的响应 ('backend'),我是否误解了这种行为?还是我的实现有误?
谢谢!
目录树:
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 ]] )
我原来的post:
您是否尝试过直接从浏览器 运行 您的 phar 文件? (如果您的网络服务器无法将您的 .phar 文件识别为 php 文件,您可以暂时将其重命名为 .php)。
您会发现它会起作用。如果您的 url 是 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 文件的原因,这是默认设置)你必须自己构建重定向部分。
如果您自己构建它,您可以像这样引用您的 backend/frontend 文件:
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 文件中的代码,运行s 以防你没有 phar 流包装器。
如果您在没有 phar 流包装器的情况下使用 运行 的代码,它会像预期的那样工作。
试试这个:
- 将您的 phar 重命名为 .php
- 使用这个 index.php:
代码:
stream_wrapper_unregister('phar');
include('./build/app.php');
您会看到它现在按预期工作(但速度较慢,因为您不再使用内置流包装器)。
我的问题是关于 createDefaultStub
的 $indexfile
和 $webindexfile
。我的理解是,如果请求来自 cli,将提供 $indexfile
,而浏览器请求将提供 $webindexfile
。
我从任一来源得到相同的响应 ('backend'),我是否误解了这种行为?还是我的实现有误?
谢谢!
目录树:
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 ]] )
我原来的post: 您是否尝试过直接从浏览器 运行 您的 phar 文件? (如果您的网络服务器无法将您的 .phar 文件识别为 php 文件,您可以暂时将其重命名为 .php)。
您会发现它会起作用。如果您的 url 是 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 文件的原因,这是默认设置)你必须自己构建重定向部分。
如果您自己构建它,您可以像这样引用您的 backend/frontend 文件:
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 文件中的代码,运行s 以防你没有 phar 流包装器。
如果您在没有 phar 流包装器的情况下使用 运行 的代码,它会像预期的那样工作。
试试这个:
- 将您的 phar 重命名为 .php
- 使用这个 index.php:
代码:
stream_wrapper_unregister('phar');
include('./build/app.php');
您会看到它现在按预期工作(但速度较慢,因为您不再使用内置流包装器)。