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

Adam Duffield
Adam Duffield
30,494 Points

Little confused when it comes to using PHP Constants to clean Sub-Directory's?

Hey All,

1st of all thanks for taking the time to help out. I'm having issues with developing a website, I want to build the site using constants like the one's used in 'PHP Enhancing a Simple Application' course.

I spent a good hour trying to make them work lastnight and I'm still having issues with understanding how it works, is there anyway I can just use some kinda of Base URL then add a filename that it will pull out and recognize without coming in and out of folders?

I managed to get 1 link working but then none of the other will, it's like the constant will only match to one specific link, I'm currently on a windows using xxamp if that is of any use to your answers.

Adam

Philip Cox
Philip Cox
14,818 Points

It would help if you could post some of your code in as well please.

Adam Duffield
Adam Duffield
30,494 Points

My bad, basically this is the file structure im working with:

htdocs/Projects/Portfolio > View/stylesheet.css > index.php Controller > AboutMe.php inc/ >Header.php

I'm using define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/Projects/Portfolio/"); to try and link to other pages. I can manage to link it up so that my index.html works but then my stylesheet isnt connected properly and I cant connect to aboutme.php and vice versa. Personally I don't think i understand how I can hook up all my links without having an indivual header at the top of every file specifying where my links are from a relative position.

1 Answer

Hi Adam,

I think you may be mixed up between the ROOT_PATH and BASE_URL constant. To see what each is, you just need to echo each constant to the page ;) If you've learned ROOT_PATH from "build a php application" with Randy, you should've come across BASE_URL also.

If you're pulling up files from the server like include files (headers and footers) you need to use ROOT_PATH.

If you're trying to link to another page, you need to prefix your url BASE_URL, and define a constant for that also.

e.g.

<?php

include( ROOT_PATH . 'inc/header.php);

?>

<link rel="stylesheet" href="<?php echo BASE_URL; ?>assets/stylesheets/my_awesome_styles.css"/>

<a href="<?php echo BASE_URL; ?>aboutme.php">About Me</a>

You may be getting more bugs because all of this is written in php - and you mentioned above that your index file is index.html. The server won't bother trying to interpret your php code because you haven't told it that's what your file contains :-).

You have to make sure all of your files with php code have an extension file_name.php

Hope this helps,

Tom