1

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
Alex
  • 737
  • 1
  • 9
  • 19

4 Answers4

8

You're looking for the BaseUrl helper.

<?php echo $this->baseUrl('images/logo.jpg'); ?>
Richard Nguyen
  • 1,203
  • 8
  • 18
2

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.

Community
  • 1
  • 1
mercator
  • 28,290
  • 8
  • 63
  • 72
  • I second this solution because it does not add the overhead of invoking the baseUrl helper. – Gordon Nov 14 '09 at 16:56
  • What happens if his website is in a subdirectory? `http://myhost.com/blah/blah/public/` => `http://myhost.com/images/logo.jpg` – Richard Nguyen Nov 14 '09 at 16:58
  • 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 Nov 14 '09 at 17:03
  • Fair call, a `base` element will also do it :) – Richard Nguyen Nov 14 '09 at 18:29
1

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
  • 61,857
  • 6
  • 82
  • 92
0

Try This:

Code:

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

Html:

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

:)

PHP Ninja
  • 1,055
  • 2
  • 9
  • 28