简体   繁体   English

PHP:从html引用phar存档中的静态文件

[英]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: 我做的是这样的:

  • Created a phar archive containing my classes and html codes. 创建了一个包含我的类和HTML代码的phar存档。 i created two stubs, for cli access and operations in cli.php, and for web access, i wrote index.php (this file requires other php files and evidently echoes html codes). 我创建了两个存根,用于cli访问和cli.php中的操作,对于web访问,我写了index.php(这个文件需要其他php文件,显然回应了html代码)。

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.
  • a configuration folder for stuff... you know :) 东西的配置文件夹...你知道:)
  • assets folder for images,styles and stuff. 资产文件夹的图像,样式和东西。
  • add an index.php file that includes the phar archive. 添加包含phar存档的index.php文件。 i couldn't run phar archive directly. 我无法直接运行phar存档。

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. srcvendor包含类, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM