资源路径相对时,无法使用包含功能 [英] Impossible to use include function when resource paths are relative

查看:41
本文介绍了资源路径相对时,无法使用包含功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我目前对PHP还是很陌生,可以动态生成内容,我正在使用PHPStorm,并使用XAMPP的PHP解释器和PHPStorm的内置Web服务器.

Background: I'm currently very new to PHP and generating content dynamically, I'm using PHPStorm and using XAMPP's PHP interpreter combined with PHPStorm's built-in web server.

问题:我最近学会了非常方便的功能 include(),现在我动态地添加了我的内容:标题内容,导航和页脚.当我在根目录的index.php中执行此操作时效果很好,因为我在导航中使用了相对路径,例如脚本中的图像和脚本路径.

Problem: I've recently learnt the very handy function include() with this I am now dynamically including my: head content, navigation and footer. This works well when I'm doing it in the index.php in the root directory, because I use relative paths for things like images in the navigation, and script paths.

但是,当我为新页面(目录)创建一个文件夹(例如"article"),然后在其中使用相同的include函数创建index.php时,使用相对路径的资源将无法使用源文件,因为我现在的目录深为1,并且从我要包含的文件中指定了相对路径?

But when I create a folder for a new page (directory) for example 'article' and then I create an index.php in there that uses the same include functions, the resources that were using relative paths won't be able to source their files because I am now 1 directory deep and the relative paths are specified from within the files I am including?

例如,在我的navigation.html中,我有< img alt ="Logo" title ="Logo" src ="img/brand/Logo.png"> 服务器的根目录,因为该路径是正确的,但是文件中包含的文件路径不正确.

For example in my navigation.html I have <img alt="Logo" title="Logo" src="img/brand/Logo.png"> This will work in the root directory of the server because the path is correct but one included within documents further down in folders it will not.

有人可以解释使用导航时的最佳做法吗,我唯一想到的就是复制我所包含的库,并设置一组带有相对路径的目录,这些路径可以返回+1目录,以便在页面上使用必须在文件夹中

Can someone explain the best practice when working with including navigation, the only thing I can think of is to duplicate the libraries I'm including and make a set with relative paths that go back +1 directories for use on the pages that have to be within folders

推荐答案

最简单的方法是使用根相对路径.如果每个链接都以"/"开头,则将其相对于根目录读取.因此,在mywebsite.com上,无论当前页面有多少个文件夹,指向"/folder/file.php"的链接将始终转到"mywebsite.com/folder/file.php".

The simplest way to achieve this is with a root-relative path. If you begin every link with a "/" it is read as relative to the root directory. Therefore on mywebsite.com, a link to "/folder/file.php" will always go to "mywebsite.com/folder/file.php" regardless of how many folders deep the current page is.

这非常适合服务器上的实时站点.我正在努力解决的问题是,它在我的本地XAMPP设置(我的测试服务器)和域名还没有生效的共享主机设置中失败,因为在两种情况下,服务器都将根目录映射到错误的位置放置在现场,但可以完美解决您的问题.

This works great for live sites on a server. The problem I'm struggling to work out is it fails in my local XAMPP setup (my testing server) and on a shared hosting setup where the domain name has not gone live yet because in both cases the server maps the root directory to the wrong place but on a live site, it solves your problem perfectly.

这篇关于资源路径相对时,无法使用包含功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆