Best approach to Manage Paths?

This question is 1/2 PHP and 1/2 HTML…

What is the best way to create Paths for things like Includes, HTML Links, and HTML Images?

In the past I used a “config” file that looked like this…


<?php
	define('ENVIRONMENT', 'development');
	//define('ENVIRONMENT', 'production');

	// File Root
	define('ROOT', ENVIRONMENT === 'development'
					? '/Users/user1/Documents/DEV/++htdocs/01_MySite/'
					: '/var/www/vhosts/MySite.com/httpdocs/');

	// Web Server Root
	define('WEB_ROOT', ENVIRONMENT === 'development'
					? 'http://local.dev1/'
					: 'http://www.MySite.com/');

	// Secure Web Server Root
	define('SECURE_WEB_ROOT', ENVIRONMENT === 'development'
					? 'http://local.dev1/'
					: 'https://www.MySite.com/');
?>

Here is the folder structure that I currently have…


Web Root
	config
		config.inc.php
	css
		top_menu.css
		components.css
	components
		body_header.inc.php
	articles
		postage_meters_save_money.php


And here is how my code currently is…

postage_meters_save_money.php


	<!-- Page Stylesheets -->
	<link type="text/css" rel="stylesheet" href="../css/top_menu.css" />
	<link type="text/css" rel="stylesheet" href="../css/components.css" />


<body>
	<!-- Access Constants -->
	<?php	require_once('../config/config.inc.php');	?>

	<div id="wrapper" class="clearfix">
		<div id="inner">
			<!-- Include BODY HEADER -->
				<?php	require_once(ROOT . 'components/body_header.inc.php');	?>

body_header.inc.php


			<div id="header">
				<a href="<?php echo WEB_ROOT ?>index.php">
					<img id="logo" src="<?php echo SECURE_WEB_ROOT ?>images/DebbiesLogo.png" />
				</a>


				<ul id="topMenu">
					<li <?php if($page=="index.php") echo ' class="current"'; ?>>
						<!-- Remove WEB_ROOT after testing??  -->
						<a href="<?php echo WEB_ROOT ?>index.php">Home</a>
					</li>
					<li <?php if($page=="case_studies.php") echo ' class="current"'; ?>>
						<a href="<?php echo WEB_ROOT ?>case_studies.php">Case Studies</a>
					</li>

Since I have learned how to create a “Virtual Server” - thanks to the gang here at SitePoint - I really don’t need my “config” file for switching back and forth between my Development and Production Environments. However, at the same time, I thought it was nice to tie things back to ROOT/WEB_ROOT/SECURE_WEB_ROOT as seen above.

I get really confused if my HTML/PHP should refer to an “absolute” path

	<link type="text/css" rel="stylesheet" href="/css/top_menu.css" />

or if it should rely on a “relative” path

	<link type="text/css" rel="stylesheet" href="../css/top_menu.css" />

In many cases, either will work, but I suspect that there is a better/best approach…

Hope that makes some sense?! :-/

Debbie

Use relative paths within your site. Absolute paths are for when the domain name is different.

Anyone else?

Debbie

the second goes up a level to the folder containing the document you are calling from, and then looks for the css folder
what if the file calling the second example is 2 folders down somewhere, then it looks up one and can’t find what it wants

i guess it all really depends on how you use implement it

absolute paths should be called with the entire address
i.e. “http://example.com/css/top_menu.css”"


Web Root
	config
		config.inc.php
	css
		top_menu.css
		components.css
	components
		body_header.inc.php
	articles
		postage_meters_save_money.php

That setup seems wrong to me.

Your PHP includes are nothing to do with css, nor js nor even the PHP files that include() or require() components.

Instead it could be:


includes 
	config
		config.inc.php
	components
		body_header.inc.php

Web Root
	css
		top_menu.css
		components.css
	articles
		postage_meters_save_money.php

For your PHP scripts the first leading fwd slash generally means “go to the top of this hard drive then work your way down”.

For your HTML (as shown in css href rules) the first leading fwd slash means “go to the top of this domain then work your way down”.

The include files such as config.inc.php which PHP depends upon will generally contain data which you would not want to make public, so you keep it out of your Web Root folder and out of that tree.

A setup more like this would make more sense to me:


/ htdocs/  <- the folder that everything to do with websites is kept

/ htdocs/includes  <- the folder outside of public-facing websites projects

	 /config
		config.inc.php
	/components
		body_header.inc.php

/ htdocs/websites/website1/  <- a single project

	/css
		top_menu.css
		components.css
	/articles
		postage_meters_save_money.php

/ htdocs/websites/website2/ <- another single project

	/css
		top_menu.css
		components.css
	/forum
		posts.php

That way you can also create folders in /includes for each website if they have different include requirements, and you can therefore build up a library of shared compenents in /includes that are likely to be used in more than one of your projects.

I am on a virtual Linux server and out-of-the-box do NOT have the ability to create folders outside of the Web Root. (Down-the-road, if you or anyone wants to give me a crash-course in do-it-yourself Linux server administration, feel free!!!)

So, for now, everything is in Web Root.


I appreciate your recommendations, but even if I followed them, they don’t really address my original question which was “Why would you want to use Absolute Paths versus Relative Paths when you reference PHP Includes, SRC’s, HREF’s, etc.??”

On the surface it seems like “Six of one, a half-a-dozen of the next”, but I suspect that one way is a better approach.

And I am content leaving my code as it was like this…


	<div id="header">
		<a href="<?php echo WEB_ROOT ?>index.php">
			<img id="logo" src="<?php echo SECURE_WEB_ROOT ?>images/Debbie_200x50.png"
					 width="220" alt="Debbie" />
		</a>

and


	<li <?php if($page=="article_index.php") echo ' class="current"'; ?>>
		<a href="<?php echo WEB_ROOT ?>article_index.php">Articles</a>
	</li>

For your PHP scripts the first leading fwd slash generally means “go to the top of this hard drive then work your way down”.

For your HTML (as shown in css href rules) the first leading fwd slash means “go to the top of this domain then work your way down”.

Okay, that helps.

The include files such as config.inc.php which PHP depends upon will generally contain data which you would not want to make public, so you keep it out of your Web Root folder and out of that tree.

Yes, that is another and a LARGER issue that I’ll have to tackle soon enough?! :blush:

Thanks,

Debbie

In that case simply read /htdocs/ in my example to be /Web root/

But that doesn’t answer my original question nor my follow-up questions…

Debbie