定义文件目录; XAMPP

I'm running XAMPP for Mac.

When I first started working with PHP, I would define my directory with a variable:

 $root = "http://localhost/~aaron/SiteFolder/"; // Dev
 $root = "http://www.sitename.com/";            // Live

This will work, but it's a huge annoyance having to comment one out, depending on if I'm editing the dev site or the live site.

I'm slowly transitioning over to OOP habits and want to maximize efficiency if at all possible. So I'm attempting to define ROOT with the current directory as shown here:

define("ROOT", dirname( __FILE__ ) . DIRECTORY_SEPARATOR);

This outputs:

/Users/aaron/Sites/SiteFolder/

This is not entirely useful to me when I'm working on the dev site, because I want to be able to use ROOT as a prefix for my images, files, etc.

This obviously won't work:

<img src="/Users/aaron/Sites/SiteFolder/images/charizard.png">

What do I need to do to fix this problem to properly define my directory?

Based on your comments in my other answer, this is what you could do:

You could create a simple installation page and ask the user for the url where they plan to host your web app.

Then you can set this variable in a database or something.

A lot of web apps do this: Wordpress, Joomla come to mind.

EDIT:

You could also try to build the url using the php $_SERVER variable. You can get the host, port, to build the url.

Unfortunately this could be a problem if someone decides to say host your web app in something other than the root of the domain of subdomain.

Something like 'http://domain/yourwebapphere/'

You wouldn't be able to detect that without the user specifying it. If you used a single endpoint and used an htaccess for redirecting urls, you'd still need to specify a base url in the htacess for it to redirect properly.

EDIT2:

Ok, if you really want to create the url dynamically, here's some psuedo code on how to do it.

$protocol = parse_server_protocol($_SERVER['server_protocol']);
$port = $_SERVER['port']
$host = $_SERVER['server_name'];
//For use case where the app is not hosted at root of domain ("http://domain.com/test") for example
$urlroot = parse_root($_SERVER["REQUEST_URI"]);

$siteurl = buildRootUrl($protocol, $port, $host, $urlRoot);

//Now save that root url in the database.

If you are using htacess to create clean urls, your app would need to know how to write the baseurl in the instance where it is not hosted at the root of the domain.

Since that is a url for the image tag, can't you just define your image root directory as define('ROOT', '/images/')?

Right now you are getting the directory path.

EDIT:

Sorry I misread the question a bit. Assuming you are talking about public resources like images on your webserver that you want to link to.

Then you can define your root as your base url?

define('ROOT', 'http://localhost/') or define('ROOT', 'http://dev.sitename/') or define('ROOT', 'http://localhost/testsite/')

as CodeIgniter uses, there's a config variable in config.php file:

$config["base_path"] = "/projects/mysite/"; //relative to http://localhost

and there's a helper function :

function base_url(){
     return $config["base_path"];
}

and in your urls you use it like

if you use smarty:

<img src="{base_url()}assets/images/1.jpg">

or direct php:

<img src="<?php echo base_url();?>assets/images/1.jpg">

and when you upload your site to the web, just change your base url to:

$config["base_path"] = "/";

and all working again.