简体   繁体   English

如何在.htaccess中附加到PHP的include_path的路径

[英]How to append a path to PHP's include_path in .htaccess

Currently on my site I'm using statements like: 目前在我的网站上我使用的语句如下:

include 'head.php';
include '../head.php';
include '../../head.php';

depending on how many nested folders deep I am. 取决于我有多少嵌套文件夹。 I'm sure there is a better way to do this. 我确信有更好的方法可以做到这一点。

I'm convinced .htaccess is the solution, but I'm not sure how to go about implementing it. 我确信.htaccess是解决方案,但我不确定如何实施它。 If I insert: 如果我插入:

php_value include_path "public/html/root"

... I lose the rest of my paths ( /usr/lib/php , etc). ...我失去了其余的路径( /usr/lib/php等)。 I naively tried: 我天真地尝试过:

php_value include_path $include_path . "(path)"

but of course this didn't work. 但当然这没用。 How could I prepend or append a single path to this list with .htaccess ? 如何使用.htaccess在此列表中添加或附加单个路径?

Create in the sub-sub-directories a file .htaccess with the contents 在子子目录中创建带有内容的文件.htaccess

php_value include_path '../../includes'

or whatever include_path is right for that directory. 或者include_path适用于该目录。

Advantages: 好处:

  1. No ugly hack with multiple includes with different depth. 没有丑陋的黑客,包含不同深度的多个包含。 Just include 'head.php' . 只需include 'head.php'
  2. No dependency on editing php.ini . 不依赖于编辑php.ini
  3. No need to use set_include_path() before including. 在包含之前不需要使用set_include_path()
  4. No dependency on including constants like include INCLUDE_PATH . 'head.php' 不依赖于包括include INCLUDE_PATH . 'head.php'类的常量include INCLUDE_PATH . 'head.php' include INCLUDE_PATH . 'head.php' . include INCLUDE_PATH . 'head.php'

Price to pay: 支付价格:

You litter your sub-sub-directories each with a file .htaccess . 你把每个子目录都.htaccess了一个文件.htaccess

If you can't edit the php.ini file itself, you could add 如果你不能编辑php.ini文件本身,你可以添加

set_include_path(get_include_path() . PATH_SEPARATOR . $path_to_add);

before your include statements. include声明之前。

However if you find yourself having include or require troubles, it is probably a code smell. 但是,如果您发现自己有includerequire麻烦,那可能就是代码味道。 The best solution would be a good object-oriented design with a good __autoload function. 最好的解决方案是良好的面向对象设计,具有良好的__autoload功能。

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

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