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

2022-01-25 00:00:00 directory include php

Ok, I am creating an admin interface for my custom blog at the 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.

Thanks, Nico

解决方案

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.

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");

相关文章