Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

Introducing Constants (live vs localhost)

So... In "Enhancing a Simple PHP Application > Cleaning URLs with Subfolders > Introducing Constants", Randy teaches us how to define the BASE_URL & ROOT_PATH constants. But, this method assumes that every time I deploy my website, I will have to first change those constants to work with the production site, and then change them back, to be able to keep working on localhost. Is there a better way of doing this? Like, for example, an automatic way to identify weather I'm on localhost or production and, based on that, have the constants change, using something like an if/else of sorts?

4 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I've seen two main ways of handling this.

  1. Have a conditional in your configuration file (as you suggest).
  2. Deploy everything except the configuration file.

Either option can work. Your deployment process and the number of environments will determine which approach makes the most sense. Option #1 makes it less likely that you will overwrite your config file if you are using FTP, but I usually use option #2 with ignore statements set in source control.

The first result on Google for php detect localhost returned this answer on Stackoverflow on how to auto detect internal/external development environment

Is the good solution to define constants like this

define("BASE_URL", '/root/shirts4mike/test/');

define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . BASE_URL);

And when moving to another folder or host, only changes applies to BASE_URL ?