开发者

Zend_framework - relative paths of images

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 t开发者_如何转开发he 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.


You're looking for the BaseUrl helper.

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


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.


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.


Try This:

Code:

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

Html:

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

:)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜