Zend_framework - 图像的相对路径
当我在 Zend_framework (php) 上开发 Web 应用程序时,我必须包含图像(css 文件、js 文件等)。
包含的解决方案是每次需要图像时指定绝对路径(我将其存储在主机的“global like”参数中,并将其与主机上图像的相对路径连接起来)。据我了解,这个解决方案价格过高(对于每张图片都要经过 DNS 服务等),我正在寻找一种更简单的解决方案来解决相对路径问题。
PS 这个问题可以在下面的例子中更好地描述:当我要去“http://myhost.com/”图像将正常显示(图像路径为:“./images/logo.jpg”)。但从“http://myhost.com/users/”网址中看不到它(为了使它可见,我必须将图像路径更改为:“./../images/logo.jpg”。
如果有人知道我如何解决这个问题,我会很高兴听到
。
When i am developing a web application on Zend_framework (php) i have to include images (css files, js files, etc).
The solution for inclusion was to specify the absolute paths each time i needed an image (i was storing in "global like" parameter with my host and concatenated it with the relative path of the image on the host). As i understand this solution is overpriced (for each picture to go the DNS service and so on) and i am looking for a simpler solution to the relative path problem.
P.S. The problem can be better described in the following example: when I am going to "http://myhost.com/" the images will be shown normally (the image path was: "./images/logo.jpg"). but it won't be seen from the "http://myhost.com/users/" url (to make it seen i had to change to image path to : "./../images/logo.jpg".
Id anybody knows how i can solve this problem, i will be glad to hear.
Gorelik.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在寻找 BaseUrl 帮助者。
You're looking for the BaseUrl helper.
您应该使用站点根相对路径而不是相对路径。
只需删除图像路径中的那个点:
./images/logo.jpg
应该是/images/logo.jpg
。前导点使其成为相对于当前 URL 的路径(因此需要根据您在网站上的位置进行更改)。没有点,它是相对于根的路径,因此它可以在任何地方工作,无需更改它。另请查看此相关问题及其答案中的链接。
You should be using site-root-relative paths instead of relative paths.
Simply remove that dot in your image path:
./images/logo.jpg
should be/images/logo.jpg
. The leading dot makes it a path relative to the current URL (so it needs to be changes depending on where you are on the site). Without a dot it's a path relative to the root, so it works everywhere without any need to change it.Also have a look at this related question and the links in its answers.
我不相信这是真的。
即使您使用完整的绝对 URL(包括主机名),也不会在每个图像上进行 DNS 查找。请放心,客户端操作系统(在某些情况下,我认为是浏览器)正在为您缓存这些内容。
当然,没有理由使用完全限定的 URL。只需从服务器根目录进行绝对操作:
如果您的应用程序在 Web 根目录中运行,只需将 DITE_ROOT_URL_PATH 设置为 ''
您甚至可以创建一个小辅助函数来节省一些击键:imgurl('foo.jpg')。
我已经使用这个方法很多年了,从来没有出现过任何头痛的情况。除非我在问题中遗漏了一些东西,否则这就是要走的路。
I don't believe that is true.
Even if you were using full absolute URLs, including the hostname, you wouldn't incur a DNS lookup on every image. Rest assured that the client OS (and in some cases, I think, the browser) is caching that stuff for you.
Of course, there's no reason to use full-qualified URLs. Just go absolute from the server root:
If your app is running in the web root, just set DITE_ROOT_URL_PATH to ''
You could even create a little helper function to save you some keystrokes: imgurl('foo.jpg').
I've used this method for years, and never had any headaches. Unless I'm missing something in the question, this is the way to go.
试试这个:
代码:
Html:
:)
Try This:
Code:
Html:
:)