This seems like the most basic issue in PHP, but I can't figure out the best way to do this. In short, I am working on upgrading an existing site and instead of Dreamweaver's clunky template function, am switching to PHP includes for various constant parts of the site (header, footer, main menu). What is tripping me up is a good, clean, simple way of intelligently defining relative paths, especially with the main menu. Obviously relative paths get all messed up, depending on where the file that's calling menu.php is in realtion to the root. So links that work on the home page, get messed up in pages in directories and subdirectories, and vice versa.

I'd like to stick with relative paths rather than absolute (if for no other reason than to make moving from my development server to the live server easier).

(Related, is there any way to get the includes to be similarly "intelligent", so I can essentially create on template that is smart enough for it to figure out where the file is in relation to the root to make include calls work with relative links.)

Thanks, espcially if this is such a basic question I should be ashamed in asking it.

Jim.

Recommended Answers

All 5 Replies

It depends on your filesystem.
What are you trying to accomplish?
What does your folderstructure look like (roughly).

The file structure is pretty simple in this case:

index.php
folder1 > files.php
folder2 > files.php
folder2 > subdirectory1 > files.php
... etc ...

I don't foresee a case where anything would be buried much deeper.

The minimum I am looking for is a clean way to create the site navigation menu in a single php file with relative links. I can do absolute links if need be, but I'd prefer not to and can't believe there isn't a good way to do this. (I'm also, ideally, looking for a method that does not require the site to be hosted at the root of the domain. When it goes live, this site will be... but during development, that is not the case and I'd like something that does not involve changing too much to go from development server to live.)

If I have a bunch of includes that every page needs, I'll make an includes.php file that has all these includes listed in it through relative paths, and then have other files include includes.php. This lets me hold on to relative paths for most of the includes.

Are the includes dynamic php includes? Or just strings that can be built dynamically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.