开发者

How to handle linking without hardcoding (relative, absolute, or using a media_url)

开发者 https://www.devze.com 2023-01-17 11:14 出处:网络
I have a website that uses php includes, for thing like the header and navigation.I am linking to a lot of css /js files in the header, obviously.I also want to be able to move the website into a subf

I have a website that uses php includes, for thing like the header and navigation. I am linking to a lot of css /js files in the header, obviously. I also want to be able to move the website into a subfolder of another site, so it can be located at www.example.com/somefolder/MySite, so I can't hardcode my links to be relative to the root.

I can't use relative URLs because the header is included on many pages in many different folder depths, so any relative links will break.

开发者_如何学Python

I have found finding a solution to this extremely frustrating. I ended up creating a MEDIA_URL constant like in django, so every one of my links looks like

<script src="<?php print MEDIA_URL ?>/css/styles.css" />

However, the disadvantage here is that now there is application logic all over the site, and it makes things like editing in dreamweaver much harder for the designers. A side benefit, however, is that now if I ever need to switch my static content to a CDN I can do this with a simple change to a variable.

Am I missing something easy here? Is there no better solution?


Why not just inform them they should code in code mode, not design mode?

0

精彩评论

暂无评论...
验证码 换一张
取 消