简体   繁体   English

PHP-调整来自外部php文件的URL链接

[英]PHP - adapt url links from external php files

Context 语境

Let's say I have the following directory context: 假设我有以下目录上下文:

.
├── index.php
├── other.php
├── nav.php
└── subfolder
    └── foo.php

My nav.php contains a simple nav element for every page of my site like 我的nav.php为网站的每个页面都包含一个简单的nav元素,例如

<nav>
    <ul>
        <li><a href="index.php">Main</a></li>
        <li><a href="other.php">Another</a></li>
    </ul>
</nav>

Problem 问题

Now, as my nav php file is meant to be universal, I want to be able to use it on php file that are in subfolders of my website, like foo.php. 现在,由于我的nav php文件具有通用性,因此我希望能够在我的网站的子文件夹(如foo.php)中的php文件中使用它。 The problem is, including nav.php in foo.php won't give me the excepted result, as the php code is just copy pasted in foo, the url links being relative, the server won't find the files index.php and other.php. 问题是,在foo.php中包含nav.php不会给我例外的结果,因为php代码只是复制粘贴到foo中,URL链接是相对的,服务器找不到文件index.php和other.php。

What I tried 我尝试了什么

I can't use absolute path with $_SERVER["DOCUMENT_ROOT"], because my website is stored on a university server where every student's website is in a personal folder, with a symbolic link from the actual server root to the student's folder. 我不能在$ _SERVER [“ DOCUMENT_ROOT”]中使用绝对路径,因为我的网站存储在大学服务器上,每个学生的网站都位于个人文件夹中,并带有从实际服务器根目录到学生文件夹的符号链接。 So, if I try to get the real absolute path, the server try to access it with a symbolic link and don't understand it. 因此,如果我尝试获取真实的绝对路径,则服务器会尝试使用符号链接来访问它,而不理解它。

I tried to use a different approach, by calculating the base of the website url (from any folder), and then explicitly asking for a absolute link with the double slash syntax ('//' + base + 'index.php'). 我试图使用一种不同的方法,即计算网站URL的基数(从任何文件夹中),然后显式地要求使用双斜杠语法('//'+ base +'index.php')进行绝对链接。 The problem of this approach, while it works, is being forced to have each php file defining a global variable storing this base url, or having a single php file with this big definition and include it in any php file. 这种方法的问题虽然有效,但被迫让每个php文件定义一个存储此基本url的全局变量,或者使单个php文件具有如此大的定义并将其包含在任何php文件中。

I find the last solution pretty dirty, hacky, and not reliable in any circumstances. 我发现最后一个解决方案很脏,很乱,在任何情况下都不可靠。 So I'm asking if there is a php-native way of doing this (ie handling relative path when including files). 所以我问是否有一种php本机的方法(即在包含文件时处理相对路径)。

For anyone looking for a quick fix which can do the job: You can recursively search for your index file and add that depth in the url. 对于寻求快速修复的人,您可以:递归搜索索引文件,然后在URL中添加该深度。

<?php
$level = "";
while (file_exists("..") && !file_exists($level . "index.php")) {
    $level = $level . "../";
}
?>

<nav class="col-3">
    <ul>
        <li><a href="<?php echo $level . 'index.php'; ?>">HOME</a></li>
        <li><a href="<?php echo $level . 'query.php'; ?>">REQUÊTE</a></li>
        <li><a href="<?php echo $level . 'view.php'; ?>">VISUALISATION</a></li>
    </ul>
</nav>

The main benefit of this workaround is that it takes only 3 lines of code and works pretty well if you want to find a file that is at the top of the website hierarchy. 此替代方法的主要优点是,它只需要三行代码,并且如果您要查找位于网站层次结构顶部的文件,则可以很好地工作。 But it won't satisfy every need, unfortunately. 但是不幸的是,它不能满足所有需求。

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

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