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!

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 Enhancing a Simple PHP Application Cleaning URLs with Rewrite Rules Introducing Rewrite Rules

Dan Neumann
Dan Neumann
Courses Plus Student 10,318 Points

I'm really confused with php paths

Randy has written this code to access files in the same sub folder:


require_once(ROOT_PATH . "inc/products.php"); 


Why does the first "require_once" access a file in the "inc" sub folder using "../" and the next uses the constant "ROOT_PATH"? Why not access files in the same folder the same way both times?

1 Answer

The first path is a relative path to the config.php, which contains certain defined constants. One of the constants is ROOT_PATH. The reason he does this is to make it easier to refactor the code. The ROOT_PATH constant is defined as the root path in any particular deployment. Here is an example of why it is nice.

My development environment is xampp. All the projects go in the htdocs folder. Since I am working on multiple projects, I put each of those in a subfolder. The root path is http://localhost/project. But when I deploy this site live the root path is the domain: http://project.com. All of my internal links that worked so nicely in my development environment are now broken because they are not in a subfolder anymore. I can go to my config folder and change my constant in one place to fix it.

I can also rearrange the internal structure and it is easier to fix the links.

Dan Neumann
Dan Neumann
Courses Plus Student 10,318 Points

Thanks Ted - This is beginning to make sense to me better.

The part that I struggled with was when to use ROOT_PATH and when to use BASE_URL. ROOT_PATH is used for the internal programming to describe where things are. BASE_URL is used to describe hyperlinked items.