使 php 包含在子目录中的工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1180459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:23:18  来源:igfitidea点击:

Making php includes work in a sub-directory

phpdirectoryinclude

提问by Nico Burns

Ok, I am creating an admin interface for my custom blog at the url /admin.

好的,我正在 url /admin 为我的自定义博客创建管理界面。

Is it possible for me to be able to use the same includes (including autoload), as the root directory.

我是否可以使用与根目录相同的包含(包括自动加载)。

If possible, I would also like to be able to automatically correct the links in the navigation so that they go that index.php in / changes to ../index.php when accessed from /admin.

如果可能,我还希望能够自动更正导航中的链接,以便在从 /admin 访问时将 / 中的 index.php 更改为 ../index.php。

Thanks, Nico

谢谢,尼科

回答by Tyler Carter

The best practice for this is to define a 'ABSOLUTE_PATH' constant that contains the directory that everything is located under. After that, you can simply copy and paste everything, because it is defining the 'full' path, which doesn't change from directory to directory.

最佳做法是定义一个“ABSOLUTE_PATH”常量,其中包含所有内容所在的目录。之后,您可以简单地复制和粘贴所有内容,因为它定义了“完整”路径,不会在目录之间更改。

Example

例子

define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);

or

define("ABS_PATH", dirname(__FILE__));
// This defines the path as the directory the file is in.

Then at any point you can simply do this to include a file

然后在任何时候您都可以简单地执行此操作以包含文件

include(ABS_PATH . "/path/to/file");

回答by Pascal MARTIN

Easiest way would be to use absolute pathes / URLs.

最简单的方法是使用绝对路径/ URL。

For the URLs, define a constant/variable somewhere, that points to the root of your application, like :

对于 URL,在某处定义一个常量/变量,指向应用程序的根目录,例如:

define('ROOT_URL', 'http://www.example.com');

or

或者

$root_url = 'http://www.example.com';

And use it in every link, like :

并在每个链接中使用它,例如:

<a href="{$root_url}/my-page.php">blah</a>

This way, always OK (and the day you install your project on another server, or in a subdirectory, you only have one constant/variable to modify, and everything still works)

这样,总是可以的(并且在您将项目安装在另一台服务器上或子目录中的那一天,您只有一个常量/变量需要修改,并且一切仍然有效)

For includes / requires, always use absolute pathes too ; one solution is to use dirname, like this :

对于包含/需要,也总是使用绝对路径;一种解决方案是使用dirname,像这样:

include dirname(__FILE__) . '/my_file.php';
include dirname(__FILE__) . '/../my-other-file.php';

__FILE__is the current file, where you are writing this line ; dirnamegets the path (the full path) to the directory containing that file.

__FILE__是当前文件,您在其中编写此行;dirname获取包含该文件的目录的路径(完整路径)。

With that, you never have to worry about the relative paths of your files.

有了它,您永远不必担心文件的相对路径。

回答by Ivan Novak

Yet another answer would be similar to combining the first two suggestions. You could define the constant:

另一个答案类似于结合前两个建议。您可以定义常量:

define('ABSPATH', dirname(__FILE__).'/');

Then, assuming that config.phpneeds to be included in many files of the site then you can use the following statement to accomplish this:

然后,假设config.php需要包含在站点的许多文件中,那么您可以使用以下语句来完成此操作:

include(ABSPATH.'config.php');

Hope this helps.

希望这可以帮助。

回答by DragonKyn

Another option that I've used for functions.php in the past is a class autoloader.

我过去用于 functions.php 的另一个选项是类自动加载器。

//Class Autoloader
spl_autoload_register(function ($className) {

    $className = strtolower($className);
    $path = "includes/{$className}.php";

    if (file_exists($path)) {

        require_once($path);

    } else {

        die("The file {$className}.php could not be found.");

    }
});

This works under certain circumstances and has been helpful to me in the past when I don't want to define an absolute $root url.

这在某些情况下有效,并且过去当我不想定义绝对的 $root url 时对我很有帮助。