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