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

How am I supposed to link other pages in my site with php? I have them stored in pages folder.

I have my pages stored in a folder called pages. home.php, contact.php, and so on. I created a navigation menu, but I saw something once where someone referenced their pages in a weird way. I was wondering what was the proper way to link to those pages using php?

Also, I want the page to load with my domain.com/page. Sometimes I see pages with domain/page.php or domain/p=?page.php or something crazy like that.

Is anyone familiar with anything like that?

1 Answer

As far as referencing pages, don't make it harder than it needs to be. If this is a simple PHP site, with static links that won't change (like a menu) than simply hard coding in their href is fine. You could probably get away with using relative hard-coded URLs

Is the Basic PHP lessons here, we learn how to define a constant, which I believe was called BASE_URL, and than use the BASE_URL constant, plus the page name to generate an absolute link path. The nice part here is it didn't create problems with incorrect relative paths.

The PHP code for that looked something like

<a href="<?php ehco BASE_URL . 'contact.php' ;?>">Contact</a>

PHP frameworks or CMS might have their own way to do things. For instance in wordpress you might use

<?php
  $contact = get_page_by_title( 'Contact' );
?>
<a href="<?php echo get_page_link( $contact->ID )  ;?>">Contact</a>

or sometimes as simple as

<?php the_permalink();?>

if used in a wordpress loop.

To address how to get what is called clean URLs.

The easiest way is to from your root folder to have your contact in a contact folder, and name your contact.php index.php. Have your page.php file in a page folder, and name the file index.php too. Than you link to that folder and the browser will look for the index file in that folder and will not display the index.php in the URL on most occasions.

Otherwise, you'll have to use URL rewrite rules. If your php is on an apache server, you'll use an .htaccess file to write your rewrite rules to get the clean URLs.

Both of these methods are covered in the PHP lessons here.

Wow,

Thank You for being detailed. This is great feedback. I was hoping I was being clear enough about what I was looking for. You answered my question and more. I'll be looking at those PHP Lessons as well.

Thank You again.