views:

191

answers:

3

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.

+5  A: 

You're looking for the BaseUrl helper.

<?php echo $this->baseUrl('images/logo.jpg'); ?>
Richard Nguyen
+2  A: 

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.

mercator
I second this solution because it does not add the overhead of invoking the baseUrl helper.
Gordon
What happens if his website is in a subdirectory? `http://myhost.com/blah/blah/public/` => `http://myhost.com/images/logo.jpg`
Richard Nguyen
Using a helper is the most flexible solution since he can move his site without worrying about breaking paths. The `baseUrl` helper queries the Front Controller for the current base URL. You could also write your own base URL helper using config values. *Premature optimisation blah blah :)*
Richard Nguyen
If his website is in a subdirectory he could either just use that full path. E.g. `/blah/blah/public/images/logo.jpg`. Or more likely, and that addresses your second comment as well, he could use the `base` HTML tag to set the base URL. Root-relative links resolve relative to the base, if specified. He could indeed use the BaseUrl helper to set the `base` `href` attribute. But there's no reason to use it on every link beyond that.
mercator
Fair call, a `base` element will also do it :)
Richard Nguyen
+1  A: 

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.

timdev