[英]PHP: Referencing static files that is inside a phar archive from html
I tried using phar in my application but there's a slight problem for me. 我尝试在我的应用程序中使用phar,但对我来说有点问题。 Earlier i did made a web project, put my classes and php files containing html files in a phar, but i couldn't import static files to phar and referenced them from html tags.
早些时候我做了一个web项目,将我的类和包含html文件的php文件放在一个phar中,但是我无法将静态文件导入到phar并从html标签中引用它们。 What i did was this:
我做的是这样的:
phar build conf of phing build.xml: phar构建phing build.xml的conf:
<pharpackage
destfile="./target/${phar.file.name}.phar"
basedir="./"
webstub="index.php"
clistub="index.php">
<fileset refid="pharBuild"/> ...
cli.php file is like this: cli.php文件是这样的:
<?php
if ($argv[1] === "op1") {
// do something
...
index.php file is like this: index.php文件是这样的:
<?php
if (php_sapi_name() == "cli") {
require_once "cli.php";
} else {
// prepare and print the web page. evidently some HTML tags.
So how can i reach my images, styles and js if i import it to the phar file? 那么如果我将它导入到phar文件中,我如何才能获得我的图像,样式和js? I want to put the css/ , js/ and images/ folders inside this phar and without changing the current code too much, when html page needs a image (like
<img src="IMAGE-LOCATION-I-DONT-KNOW">
), it will get it from the file that is inside my phar file. 我想将css /,js /和images /文件夹放在这个phar中,而不需要更改当前代码,当html页面需要图像时(如
<img src="IMAGE-LOCATION-I-DONT-KNOW">
),它将从我的phar文件中的文件中获取它。
I'm not sure why your setup isn't working, but I can access static files through a PHAR just fine. 我不确定为什么你的设置不起作用,但我可以通过PHAR访问静态文件就好了。 Here's what I've got:
这是我得到的:
build.xml
<target name="package">
<pharpackage basedir="." destfile="./my-project.phar" stub="phar_stub.php" compression="none">
<fileset dir="src">
<include name="**/**"/>
</fileset>
<fileset dir="vendor">
<include name="**/**"/>
</fileset>
<fileset dir="public">
<include name="**/**"/>
</fileset>
</pharpackage>
</target>
phar_stub.php
<?php
Phar::mungServer(['REQUEST_URI', 'SCRIPT_NAME']);
Phar::webPhar(null, __DIR__ . "/public/index.php");
__HALT_COMPILER();
src
and vendor
contain classes and public
contains an index.php
(the entry point to the application) as well as a resources
folder with css and js. src
和vendor
包含类, public
包含index.php
(应用程序的入口点)以及包含css和js的resources
文件夹。
Once deployed I can access my application via: http://localhost/my-project.phar/public/index.php/some/route 部署后,我可以通过以下方式访问我的应用程序: http://localhost/my-project.phar/public/index.php/some/route
And the static resources can be accessed via: http://localhost/my-project.phar/public/resource/app.css 并且可以通过以下方式访问静态资源: http://localhost/my-project.phar/public/resource/app.css
One thing I definitely struggled with was having "compression='gzip'", which would deliver a compressed version to the browser, but the browser couldn't figure out how to decompress it. 我肯定会遇到的一件事是“压缩='gzip'”,它会向浏览器提供压缩版本,但浏览器无法弄清楚如何解压缩它。 I turned off compression instead of playing around with it too much.
我关掉压缩而不是玩太多。 Hope that helps
希望有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.