views:

346

answers:

6

Consider the following directory structure:

  • ROOT
  • ------ images
  • ............... logo.png
  • ------ includes
  • ............... vars.php
  • ------ layout
  • ............... content.php
  • ------ index.php

How do I define a path constant for logo.png in vars.php that is accessible in both index.php and content.php? Should be compatible with HTML Tags as a relative path.

<img src="<?php echo IMAGE_PATH; ?>">

which should be parsed as

<img src="images/logo.png"> <!-- if used in index.php -->

and

<img src="../images/logo.png"> <!-- if used in content.php -->

New Question (EDIT): Does root-relative path work when including php files using include / require methods?

+2  A: 

Absolute url or root paths will give you the least amount of headaces. Trust me, when the system grows you'll regret that setup.

It is a perfectly legal way to reference things. (as you ask in the comments)

If you're worried about setups between domains, just create a config variable with the absolute path to the domain / directory / etc

Ólafur Waage
Is there a reason you prefer `absolute`, as opposed to `root-relative`, paths?
David Thomas
Setup between domains is not the issue. But doesn't using absolute paths for HTML tags (images/stylesheets) carry a performance penalty?
gAMBOOKa
I believe it does, which is one of the reasons I prefer root-relative.
David Thomas
using absolute paths everywhere is always going to add weight to your pages (and css for that matter). Simply put, 'http://www.mydomain.com/' is longer than '/'
WibblePoop
I am talking about the concept in general.
Ólafur Waage
A: 

I'd suggest, primarily, that you use root-relative paths. This is only to reduce the complications of moving your site to another host, and also it allows for consistent paths (rather than using an if() condition to test from where the script's being run).

But otherwise, your suggestion would be fine.

David Thomas
How do I get root-relative paths?
gAMBOOKa
NVM, DisgruntledGoat answered it!
gAMBOOKa
+1  A: 

You can use "root-relative" paths. Simply link to everything with a forward slash at the beginning, i.e.

<img src="/images/logo.png">

This will resolve to http://yoursite.com/images/logo.png from every page on yoursite.com.

DisgruntledGoat
And called a 'root-relative' path (to the best of my knowledge, anyway). =)
David Thomas
@ricebowl: No, it’s an *absolute path*.
Gumbo
A: 

simply specify all paths as relative to the root

<img src="/images/logo.png"> <!-- will work anywhere  -->
WibblePoop
+3  A: 

Try setting the <base> tag in the <head> section of your code.

All your images, css, and js files will use this instead of the url in the address bar.

Info on base

Scott
And url's would be like `images/blah.png`, without a leading `/`.
strager
I like your thinking but in certain cases, where modular components are concerned, the paths might need to be relative to the module directory rather than the root directory.
gAMBOOKa
@gAMBooka, your right but those cases probably occur less often and could be hard coded.
Scott
But again, that's an absolute url and brings a dns lookup penalty that I'd like to avoid. Crap! I'm all confused now!
gAMBOOKa
Note that changing the base URL will affect any relative URL and not just relative URLs with a relative URL path.
Gumbo
A: 

I would use something like an application base URL:

define('APP_URL', 'http://example.com/path/to/app');
echo '<img src="'.APP_URL.IMAGE_PATH.'">';

Or to have it more convenient, write a function that resolves your relative URL to an absolute URL.

Gumbo