2016-09-24 92 views
3

我嘗試在我的應用程序中使用phar,但對我來說存在一個小問題。早些時候,我做了一個web項目,把我的包含html文件的類和php文件放在phar中,但我無法將靜態文件導入phar並從html標記中引用它們。我做的是這樣的:PHP:從html中引用phar檔案中的靜態文件

  • 創建一個包含我的類和html代碼的phar存檔。我創建了兩個存根,cli.php中的cli訪問和操作,以及web訪問,我寫了index.php(這個文件需要其他的php文件,並且明顯地回顯了html代碼)。 phing的build.xml

藥業打造的conf:

<pharpackage 
    destfile="./target/${phar.file.name}.phar" 
    basedir="./" 
    webstub="index.php" 
    clistub="index.php"> 
    <fileset refid="pharBuild"/> ... 

cli.php文件是這樣的:

<?php 
if ($argv[1] === "op1") { 
    // do something 
... 

index.php文件是這樣的:

<?php 
if (php_sapi_name() == "cli") { 
require_once "cli.php"; 
} else { 
// prepare and print the web page. evidently some HTML tags. 
  • 配置摺疊呃東西...你知道:)
  • 資產文件夾的圖像,樣式和東西。
  • 添加一個包含phar存檔的index.php文件。我無法直接運行phar歸檔。

那麼如何將圖像,樣式和js導入phar文件?我想將css /,js /和images /文件夾放在這個phar中,並且在html頁面需要一個圖像時(如<img src="IMAGE-LOCATION-I-DONT-KNOW">),不用更改當前代碼,它會從phar文件中獲取它。

回答

1

我不知道爲什麼你的設置不工作,但我可以通過一個PHAR訪問靜態文件就好。下面是我得到了什麼:

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(); 

srcvendor含有類和public包含index.php(入口點的應用程序),以及一個resources用css和js文件夾。

一旦部署完成,我可以通過訪問我的應用程序:http://localhost/my-project.phar/public/index.php/some/route

和靜態資源可以通過以下方式訪問:http://localhost/my-project.phar/public/resource/app.css

有一件事我絕對是有「壓縮=‘gzip的’」努力,這將向瀏覽器提供壓縮版本,但瀏覽器無法弄清楚如何解壓縮它。我關閉了壓縮功能,而不是過多地使用它。希望有幫助

+0

謝謝!存根文件對我來說是個問題。我改變了它,它工作。要搜索這個mungServer的東西...祝你有美好的一天! – Kambaa

相關問題