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 Building Websites with PHP Slim Basics & Twig Templates Including & Rendering

Daniel Schroeder
Daniel Schroeder
13,859 Points

Contact page not working on localhost through Slim using Apache and Ubuntu

I'm running Ubuntu 16.04 with Apache 2.4.18 and PHP 7.0.8 and cannot display the contact page. The index.html page displays fine with this url: localhost/treehouse/build_websites_php/.

When I try this url: localhost/treehouse/build_websites_php/contact I get a 404 Not Found error, but not through Slim.

Here is my PHP code:


require 'vendor/autoload.php';

$app = new \Slim\Slim();

$app->get('/', function () use($app) {
  /* When using render(), the url 
  localhost/treehouse/build_websites_php/ to  gets you the home page. */

/* This SHOULD bring up the contact page at url 
localhost/treehouse/build_websites_php/contact, but it doesn't! */
$app->get('/contact', function () use($app) {


My directory structure is the same as in the video, with my index.php in the document root and my index.html and contact.html in the templates directory.

Any help would be appreciated!

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi! I got your notification asking for my assistance. But the bad news is that I don't see anything really wrong with your code other than possibly some extraneous whitespace between function and the following parentheses. But PHP isn't generally sensitive to whitespace.

So I must ask some basics. Have you tried restarting the apache server? And are you 100% positive that your contact.html is named so (including capitalization)? I once saw someone who couldn't get a page to load because their file name started with a space!

2 Answers

Daniel Schroeder
Daniel Schroeder
13,859 Points

Jennifer, thanks for the quick response! I have tried restarting the Apache server and have checked the filename, no dice! I've also tried the VirtualHost and .htaccess configurations as suggested here. My file permissions are also not the issue. Kind of at a dead end, since the index.html file works fine through Slim, I can't understand why the contact.html file wouldn't work as well! If you have any other suggestions, I'd love to here them!

I'm running the exact same code through MAMP and having the same issue. The index works fine, but no contact page. Even later when using twig. I'm using the Slim version recommended, but no luck.