如何从正确的位置调用 CSS 文件
我正在用 PHP 启动一个项目,我想从一开始就正确地构建我的文件(与我的上一个项目不同,它几乎将每个文件都放在一个目录中)。问题如下,我将用一个例子来描述:
取以下文件:index.php、includes/header.php 和 css/common.css。 index.php“包含”标头(许多其他 php 文件也是如此)。然后标头调用 common.css,以便正确放置其 html 元素。 common.css 还将为 index.php 和其他文件中的常规元素提供样式。
请注意,由于包含了标头,因此当标头调用 common.css 时,它会从调用 it 的文件位置执行此操作;在本例中为index.php。但是,如果我添加,比如说,modules/friends.php 并用它调用标头,它将在错误的位置寻找 CSS 文件!
最初,我尝试通过使用调用 CSS 文件时的实际路径来解决此问题。但是,我的本地计算机和 Web 服务器具有不同的目录布局,因此我不能简单地调用 /var/www/whatever。
任何人都可以帮助我或将我重定向到记录此类事情的地方吗?
谢谢,
帕拉贡
I'm starting a project in PHP, and I want to structure my files properly from the start (unlike my last project, which had almost every file in a single directory). The problem is the following, which I will describe with an example:
Take the following files: index.php, includes/header.php, and css/common.css. index.php 'includes' the header (as will many other php files). The header then calls common.css so that its html elements can be placed properly. common.css will also provide styling for general elements in index.php and other files.
Notice that since the header is being included, when the header calls common.css, it does so from the location of the file calling it; in this case, index.php. But if I add, say, modules/friends.php and call the header with it, it will be looking for the CSS file in the wrong spot!
Initially I tried to remedy this by using the actual path for when I call CSS files. However, my local machine and web server have a different layout of directories, and therefore I cannot simply call /var/www/whatever.
Can anyone help me or redirect me to a place where this sort of thing is documented?
Thanks,
Paragon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
始终指定所有资源的绝对路径:
.css
、.js
、图像等...http://en.wikipedia.org/wiki/Absolute_path
你可以。 Web 路径与本地文件系统路径不同。当您在 web 中指定路径时,根符号
/
指定为webroot
(项目所在的目录),而不是文件系统根。Always specify absolute paths to all your resources:
.css
,.js
, images, etc...http://en.wikipedia.org/wiki/Absolute_path
You can. Web paths is not the same thing as local filesystem paths. When you specify path in web - the root sign
/
specifies to thewebroot
(the directory your project is placed at), not your filesystem root.恭喜你认识到了一个大问题。
是的,这始终是您一开始就需要回答的重大问题。
我终于学会了——这是在好几年之后——尽我最大的努力使开发机器(比如我的电脑)上的文件结构完全
如果你能做到这一点,那么剩下的就是小菜一碟,相信我。您可以将文件放在两台计算机上的任何目录中,只要对您有意义即可。您可以弄清楚哪些文件应该放在哪里。
如果您不愿意在两台机器上尝试几乎相同的文件目录设置,那么当您编辑时,您将永远想知道,“嘿,我在哪台机器上?如果我在主机上,那么very-important-file.php 就在/toplevel 中,其他所有内容都在它下面。但是如果我在PC 上,那么very-important-file.php 就在/my-files 中,请参阅,然后。其他文件位于不同的级别,我是否删除了该文件并且......”我的上帝,不要让我思考,更不用说思考那些无意识的废话了。
您可以处理并记住根位于不同机器上的不同位置,但除此之外,忘记它。
现在,当您运行您的东西时,您将始终知道这些东西的各个部分在哪里:CSS 文件、JS 文件等等。另外,您可以(也许;如果您幸运的话)在 PC 或 主机上同样调试您的代码,没有任何差异,也没有在任何地方进行更改。另外,当您上传新代码时,您可以将其以一大块的形式通过 FTP 传输到主机,并将其植根于您喜欢的位置。 (这有一个非常好的附带好处,您可以在开发机器上将文件移动到您想要的任何位置。)
小菜一碟!不要错过这个为自己节省几天或几周(字面意思)时间的机会。
恕我直言,总是如此。
Congratulations on recognizing a huge problem.
Yes, this is always the big, important question that you need to answer at the start.
I've finally learned -- and this is after quite a few years -- to try my best to make the file structure on the development machine (my PC, say) be exactly like the file structure on the host machine (a Linux host, for example). That one thing alone has saved me unending hours of grief.
If you can accomplish that, then the rest is a piece of cake, believe me. You can put files in whatever directories you want, wherever it makes sense to you, on both machines. You can figure out what files should go where.
If you don't bother to try for near-identical file-directory setups on both machines, you are forever going to be wondering, as you edit away, "Hey, what machine am I on? If I'm on the host, then very-important-file.php is in /toplevel, and everything else is under it. But if I'm on the PC, then very-important-file.php is over here in /my-files, see, and then other files are on different levels and did I delete that file and ..." My God, don't make me think, much less think about that mindless crap.
You can handle and remember just the root being in different spots on different machines, but other than that, forget it.
Now when you come to run your stuff, you will always know where the pieces of that stuff are: CSS files, JS files, whatever. PLUS you can (maybe; if you're lucky) debug your code on the PC or the host equally well, with no differences and with no changes anywhere. PLUS when you upload your new code, you can FTP it up to the host in one big chunk rooted where you like. (Which has the very nice ancillary benefit of your being able to move files around wherever you want on the development machine.)
Piece of cake! Don't pass up this chance to save yourself days or weeks (literally) of time.
Always IMHO.