简体   繁体   English

如何以兼容的方式包含PHP文件以便使用和不使用Phar进行打包?

[英]How to include PHP files in a compatible way for packaging with and without Phar?

Given a PHP application with the following structure: 给定具有以下结构的PHP应用程序:

/
    lib/
        mylib.php
    web/
        index.php // includes "mylib.php" with: require_once __DIR__ . "/../lib/mylib.php"

I am trying to cover the following cases at the same time with the same source base: 我试图用相同的源代码基础同时覆盖以下案例:

  1. No Phar : Being able to use the application as is, with the DocumentRoot pointing on web/ and redirecting all requests to index.php . No Phar :能够按原样使用应用程序, DocumentRoot指向web /并将所有请求重定向到index.php
  2. Minimal Phar : Being able to produce a phar that contains only web/index.php and that would be saved as: web/application-minimal.phar . Minimal Phar :能够生成仅包含web / index.php的phar,并保存为: web / application-minimal.phar
  3. Full Phar : Being able to produce a phar that contains both the content of lib directory and web/index.php and that would be saved as: web/application-full.phar . Full Phar :能够生成包含lib目录和web / index.php内容的phar,并保存为: web / application-full.phar

In the case of phar files, all requests would be redirected to the phar file itself. 对于phar文件,所有请求都将重定向到phar文件本身。

Is it possible to achieve all those use cases while not having to change the *require_once*? 是否有可能在不必更改* require_once *的情况下实现所有这些用例?

I tried different approaches (relative/absolute) to include lib/mylib.php from *web/index.php", as well as trying some tricks with Phar::mount() . None of my attempts succeeded. 我尝试了不同的方法(相对/绝对)来包含来自* web / index.php的lib / mylib.php ,以及尝试使用Phar :: mount()的一些技巧。我的尝试都没有成功。

Simply rely on your include path. 只需依靠您的包含路径即可。

Don't do: 不要这样做:

require_once __DIR__ . "/../lib/mylib.php";

but: 但:

require_once "mylib.php";

and set the include path at the beginning of your script: 并在脚本开头设置包含路径:

set_include_path(__DIR__ . '/../lib/' . PATH_SEPARATOR . get_include_path());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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