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

Pavle Lucic
Pavle Lucic
10,801 Points

Using Root-Relative Web Addresses - problem with path

Have problem with linking files with root relative method.

By adding "/" before some file in link.

My files in Xampp is organized in this way: xampp/htdocs/root/shirts4mike/

by adding "/" in header.php for example a href="/shirts.php", it will target this path xampp/htdocs/shirts.php ->cant find file

I figured out that root relative web address in my case could be "/root/shirts4mike/" . It works the same as Randy presented at the end of the video.

But, what will happen when I transfer files to the web host?

  1. Do I need to change root relative address?
  2. Is there any better solution for this?

For example to store in variable root relative web address? Or maybe to somehow define what is my root folder trought some Php method?

Help

2 Answers

An easy thing to do is create a constant (which I believe Randy covers in the videos at some point, not sure if you're there yet). Something like,

define("ROOT", $_SERVER['DOCUMENT_ROOT'] . "/");

I'm not too familiar with XAMP, but it would seem that you would want to do your local environment like this,

define("ROOT", $_SERVER['DOCUMENT_ROOT'] . "/root/shirts4mike/");

Constants are really great and Randy does indeed take you through those later in the video. Root relative paths will work on a server - after all they're relative! As long as the files don't change in relation to each other, root relative paths will work wherever. They're just a bugger to maintain, and break easily, which is why he goes through better methods in a few videos time.

Hope that helps