Zend_framework - 图像的相对路径

发布于 2024-08-11 07:46:54 字数 542 浏览 2 评论 0原文

当我在 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 技术交流群。

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

发布评论

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

评论(4

最偏执的依靠 2024-08-18 07:46:54

您正在寻找 BaseUrl 帮助者。

<?php echo $this->baseUrl('images/logo.jpg'); ?>

You're looking for the BaseUrl helper.

<?php echo $this->baseUrl('images/logo.jpg'); ?>
楠木可依 2024-08-18 07:46:54

您应该使用站点根相对路径而不是相对路径。

只需删除图像路径中的那个点:./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.

爱格式化 2024-08-18 07:46:54

据我了解,这个解决方案是
价格过高(每张照片去
DNS服务等等)我是
寻找更简单的解决方案
相对路径问题。

我不相信这是真的。

即使您使用完整的绝对 URL(包括主机名),也不会在每个图像上进行 DNS 查找。请放心,客户端操作系统(在某些情况下,我认为是浏览器)正在为您缓存这些内容。

当然,没有理由使用完全限定的 URL。只需从服务器根目录进行绝对操作:

<?PHP
// somewhere in config.php or similar:
define('SITE_ROOT_URL_PATH','/myapplication');
...
// meanwhile, in some template
<img src="<?php echo SITE_ROOT_URL_PATH . '/images/foo.jpg'; ?>" />

如果您的应用程序在 Web 根目录中运行,只需将 DITE_ROOT_URL_PATH 设置为 ''

您甚至可以创建一个小辅助函数来节省一些击键:imgurl('foo.jpg')。

我已经使用这个方法很多年了,从来没有出现过任何头痛的情况。除非我在问题中遗漏了一些东西,否则这就是要走的路。

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.

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:

<?PHP
// somewhere in config.php or similar:
define('SITE_ROOT_URL_PATH','/myapplication');
...
// meanwhile, in some template
<img src="<?php echo SITE_ROOT_URL_PATH . '/images/foo.jpg'; ?>" />

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.

陈独秀 2024-08-18 07:46:54

试试这个:

代码:

$imgurl=$this->layout()->staticBaseUrl.'application/themes/themes-files/images/filename.jpg';

Html:

<img src="<?php echo $imgurl; ?> " width="50" height="50"/>

:)

Try This:

Code:

$imgurl=$this->layout()->staticBaseUrl.'application/themes/themes-files/images/filename.jpg';

Html:

<img src="<?php echo $imgurl; ?> " width="50" height="50"/>

:)

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