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

时间:2022-09-30 22:52:10

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. If I insert:

我确信.htaccess是解决方案,但我不确定如何实现它。如果我插入:

php_value include_path "public/html/root"

... I lose the rest of my paths (/usr/lib/php, etc). I naively tried:

…我丢失了剩下的路径(/usr/lib/php等)。我天真的尝试:

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为这个列表添加一个路径?

2 个解决方案

#1


21  

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.

包括之前声明。

However if you find yourself having include or require troubles, it is probably a code smell. The best solution would be a good object-oriented design with a good __autoload function.

然而,如果您发现自己遇到了包括或需要麻烦,这可能是代码味道。最好的解决方案是一个好的面向对象设计和一个好的__autoload函数。

#2


27  

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'.
  2. 没有丑陋的黑客,有多重包含有不同的深度。只包括“head.php”。
  3. No dependency on editing php.ini.
  4. 不依赖于编辑php.ini。
  5. No need to use set_include_path() before including.
  6. 在包含之前不需要使用set_include_path()。
  7. No dependency on including constants like include INCLUDE_PATH . 'head.php'.
  8. 不依赖于包含常量,比如include INCLUDE_PATH。“head.php”。

Price to pay:

代价:

You litter your sub-sub-directories each with a file .htaccess.

每个子目录都有一个.htaccess文件。

#1


21  

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.

包括之前声明。

However if you find yourself having include or require troubles, it is probably a code smell. The best solution would be a good object-oriented design with a good __autoload function.

然而,如果您发现自己遇到了包括或需要麻烦,这可能是代码味道。最好的解决方案是一个好的面向对象设计和一个好的__autoload函数。

#2


27  

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'.
  2. 没有丑陋的黑客,有多重包含有不同的深度。只包括“head.php”。
  3. No dependency on editing php.ini.
  4. 不依赖于编辑php.ini。
  5. No need to use set_include_path() before including.
  6. 在包含之前不需要使用set_include_path()。
  7. No dependency on including constants like include INCLUDE_PATH . 'head.php'.
  8. 不依赖于包含常量,比如include INCLUDE_PATH。“head.php”。

Price to pay:

代价:

You litter your sub-sub-directories each with a file .htaccess.

每个子目录都有一个.htaccess文件。