动态添加来自不同页面不同目录深度的相同CSS文件

发布于 2024-11-03 17:17:20 字数 1916 浏览 0 评论 0原文

好吧,我已经尝试解决这个问题有一段时间了。 我正在开发一个网站,这基本上是我的结构:

index.php
page-x/
    index.php
    page-x-y/
        index.php
include/
    functions.php
css/
    main.css

除了添加更多子文件夹和索引页面。 基本上是三个不同级别的页面。

它们都需要有 css/main.css,但是任何一个索引页面的路径都会不同。

现在我使用functions.php来添加CSS,并且已经必须手动输入从索引页到functions.php的相对路径(即 require_once("../include/functions.php"); 或 ../../include

我怎样才能使functions.php能够找出从它包含的任何索引页面到css/main.css的相对路径?

IE 如何为functions.php 中的以下代码定义 $relDir

echo '<link rel="stylesheet" type="text/css" href="' . $relDir . 'css/main.css" />';

我可以手动传递 """../"“../../” 每次到该函数,但我可以避免这种情况吗


? 如果我在functions.php中定义$BASE = dirname(__DIR__);,我可以找到站点的根目录并从那里获取CSS的绝对路径。

然而,CSS 似乎没有正确链接,在我的本地计算机上,生成的链接如下所示:(我可以操作斜杠以保持一致,但这没有帮助)

"C:\Documents and Settings\Lucas\site/css/main.css"

(注意:我正在使用别名http://127.0.0.1:8888/site/ 连接到该地址,但 __DIR__ 找到文件的实际目录

。在我正在测试的(我的大学)服务器上返回

"/Volumes/Web/Students/MYNAME/Sites/MYSITE/css/main.css"

网站的实际 URL 如下所示:

http://www.SCHOOL.com/MYNAME/MYSITE/

CSS 在任何一种情况下都不会链接


编辑 2 我可以定义 $relDir as:

 str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/');

根据索引的深度返回以下内容之一:

""
"../"
"../../"

这听起来似乎最好的办法是研究适当的测试环境,并使用绝对路径。看看这个,我对其中的一些东西还是有点新手


编辑1:好吧,大多数人建议我坚持手动执行,最初有一个函数调用类似这样的东西。 :

<?php require_once("../include/functions.php");
buildHead("Music", "../"); //($title, $relDir) ?>

我只需将每个页面的两个位置的 ../ 更改为任何内容即可。

Okay I've been trying to figure this out for a while now.
I have a website I'm working on and here is basically my structure:

index.php
page-x/
    index.php
    page-x-y/
        index.php
include/
    functions.php
css/
    main.css

Except add on more sub-folders and index pages.
Basically pages at three different levels.

They all need to have css/main.css, but the path from any one index page is going to be different.

Right now I use functions.php to add the CSS, and already have to manually enter the relative path from an index page to functions.php (ie require_once("../include/functions.php"); or ../../include etc

How can I make it so that functions.php can figure out the relative path from any index page it is included on, to css/main.css?

I.E. how would I define $relDir for the following code in functions.php:

echo '<link rel="stylesheet" type="text/css" href="' . $relDir . 'css/main.css" />';

I could manually pass "" or "../" or "../../" every-time to the function, but can I avoid that?


EDIT 3
If I define $BASE = dirname(__DIR__); in functions.php, I can find the root of the site and go from there to get the absolute path to the CSS.

However the css doesn't seem to link properly, on my local machine the resulting link looks like: (I can manipulate the slashes to be consistent but it doesn't help)

"C:\Documents and Settings\Lucas\site/css/main.css"

(Note: I am using an alias on easyPHP so that I can connect to this address from http://127.0.0.1:8888/site/ but__DIR__ finds the ACTUAL directory of the file.

And on the (My universities) server I am testing on comes back with

"/Volumes/Web/Students/MYNAME/Sites/MYSITE/css/main.css"

The ACTUAL URL of the website looks like:

http://www.SCHOOL.com/MYNAME/MYSITE/

The CSS wont link in either situation


EDIT 2 I can define $relDir as:

 str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/');

returns one of the following depending on how deep the index is:

""
"../"
"../../"

This a lot of processing apparently though. It sounds like the best thing to do would be to look into proper testing environments, and use an absolute path. I'll look into that. I'm still a bit of a newb with some of that stuff.


EDIT 1: Okay most people are suggesting I stick with just doing it manually, originally had a function call something like this:

<?php require_once("../include/functions.php");
buildHead("Music", "../"); //($title, $relDir) ?>

I just have to change the ../ to whatever in both places for every page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抽个烟儿 2024-11-10 17:17:20

为什么你甚至尝试使用相对路径?你不能将其链接为 /css/main.css 吗?那么它将始终相对于站点的根目录,而不是当前目录。

Why are you even trying to use a relative path? Can't you just link it as /css/main.css? Then it will always be relative to the root of your site, rather than the current directory.

晨与橙与城 2024-11-10 17:17:20

最简单的方法?使用绝对路径。如果您的网站仅停留在单一固定路径上,那就很容易。如果您想自由地更改站点的结构,请添加一个配置参数 - 例如。在每个 PHP 文件顶部包含一个配置文件,并在其中定义一个 $BASE 参数,该参数保存应用程序库的绝对路径,然后执行以下操作:

echo '<link rel="stylesheet" type="text/css" href="'.$BASE.'/css/main.css" />'

如果 $BASE 为空,则它将默认为 Web 服务器的根目录。

您还可以使用 __DIR__ 魔术常量自动完成所有这些处理(如果您使用的是 PHP < 5.3,则使用 __FILE__ ,但它没有太大变化 - 您只需从获得的文件名中删除文件名)。如果您将此配置文件包含在每个 PHP 文件的顶部(或者您使用 apache auto prepend 指令),并且您的配置文件位于网站的基本目录中,那么这就足够了:

$BASE = __DIR__;

否则,如果它位于目录,你只需要上去,但是这是在部署时已知的并且不依赖于请求。

Easiest way? Use absolute paths. If your website just stays on a single fixed path it's easy. If you want to keep yourself free to change the structure of your site, add a configuration parameter -- eg. include a config file on top of each PHP file and define a $BASE parameter there which holds the absolute path of your app base, then do:

echo '<link rel="stylesheet" type="text/css" href="'.$BASE.'/css/main.css" />'

If $BASE is empty, it will default to the root of your webserver.

You can also do all this processing automatically, using the __DIR__ magic constant (or __FILE__ if you are using PHP < 5.3 but it doesn't change much - you just need to strip the file name from what you get). If you include this config file at the top of each PHP file (or you use apache auto prepend directive), and your config file is located in the base directory of your website, then this will be sufficient:

$BASE = __DIR__;

Otherwise, if it sits in a directory, you just need to go up, but this is known when you are deploying and is not dependent on the request.

月棠 2024-11-10 17:17:20

嗯..我建议绝对链接CSS...

否则你可以

echo str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/');

hm.. I would recommend to link the css absolutely…

otherwise you could

echo str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/');
浪漫人生路 2024-11-10 17:17:20

您必须使用 $_SERVER['DOCUMENT_ROOT'] 获取网站基目录。然后把这个值放到session中。然后你可以使用
在base index.php中

$dir = $_SERVER['DOCUMENT_ROOT'];
$include = $dir. "/includes/functions.php";
$_SESSION['include_dir'] = $include;

您可以使用其他文件

reqire($_SESSION['include_dir']);

you must get your site base directory using $_SERVER['DOCUMENT_ROOT']. Then put this value in session. Then you can use
in base index.php

$dir = $_SERVER['DOCUMENT_ROOT'];
$include = $dir. "/includes/functions.php";
$_SESSION['include_dir'] = $include;

Other files you can use

reqire($_SESSION['include_dir']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文