[英]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: 好处:
include 'head.php'
. 只需include 'head.php'
。 php.ini
. 不依赖于编辑php.ini
。 set_include_path()
before including. 在包含之前不需要使用set_include_path()
。 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. 但是,如果您发现自己有include
或require
麻烦,那可能就是代码味道。 The best solution would be a good object-oriented design with a good __autoload
function. 最好的解决方案是良好的面向对象设计,具有良好的__autoload
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.