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 Contact Form & Sending Email Named Routes

Jon Mullins
Jon Mullins
15,724 Points

Contact not working on local server.

I have an issue with the contact page that is driving me crazy. It is not working on my local server (in ubuntu). Everything works in workspaces just fine, but when I move the files to my local machine, I get a broken link on the contact page. Just to clarify, I downloaded the zip from the downloads area and copied everything into my www directory.

7 Answers

Jon Mullins
Jon Mullins
15,724 Points

So I finally figured this out. First, I started from an empty /var/www folder on my local ubuntu install. Then I watched from "A modern look at PHP" and hit a wall in "Including and running Slim". Everything worked exactly as it did for Hampton, until the point after the .htaccess file was created. from there I continued to receive the 404 error. From there there was much googling, which led me to my /etc/apache2/apache2.conf file where I changed the following:

AllowOverride None

to:

AllowOverride All

This caused another error (a 500 error). More googling ensued, and it was determined that the rewrite module in apache was not enabled. After running:

sudo a2enmod rewrite

followed by

sudo service apache2 restart

All is well, and I can now contact Waldo!

Thanks again for the help.

My XAMPP install had no such issues. GREAT JOB figuring that out.

Jon Mullins
Jon Mullins
15,724 Points

I wasn't going to let this one beat me. Knowing the code worked on workspaces, but not locally told me it was something in my local environment. Thanks for the "Best answer" vote. This is my first "Best Answer" and it was on my own question ....

That is how it works sometimes. The best answer lets everyone know the problem is solved. And when you solve it yourself, you deserve the best answer.

Hello Jon,

I downloaded it too and I dot the same error. I looked into the files and there is actually no contact.html file in there. They must have made a mistake when making the zip file.

Jon Mullins
Jon Mullins
15,724 Points

I'm not sure that's it. I believe it is looking for the contact.twig file in the templates directory, and not the .html file. But if I'm being honest, it's all a bit confusing.

What local server are you using? I am using XAMPP in Mint (a build that is based on Ubuntu) and it works fine for me. One issue is to make sure you are putting it in the correct location on your computer for the local server to see it properly.

I will also download the files and see if I can make it work on this computer. My project is on my laptop it appears.

If I remember right there is an issue with the path in the file when on a local server that is in a sub folder. I have to look at my laptop to see what the solution is, though.

Jon Mullins
Jon Mullins
15,724 Points

Thanks, I'm not at that computer at the moment. I honestly can't remember which server I installed as it has been quite a while now. Tomorrow I can have a look.

The route works if we change the get line to this:

<?php
$app->get('/contact.html', function() use($app){
  $app->render('contact.html');
});

The issue is in the request to get rerouted. I think the solution is an alias in the $app->get command, but I will have to work on that some more. I am still not on my laptop that has some of the solution. As I recall, there is still an issue with routing.

First, here is the code that works (with a bug) on the local server. The bug also exists on a live site.

<?php
// require Slim
require 'vendor/autoload.php';
// Set time zone
date_default_timezone_set('America/Los_Angeles');

//slim-views is required for Twig
//see https://github.com/slimphp/Slim-Views
//must install via composer
$app = new \Slim\Slim(array(
    'view' => new \Slim\Views\Twig()
));

$view = $app->view();
$view->parserOptions = array(
    'debug' => true
);

//the code below enables us to use the helpers below
//urlFor siteUrl baseUrl currentUrl
//documentation at https://github.com/slimphp/Slim-Views
$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$app->get('/', function() use($app) {
    $app->render("about.twig");
})->name('home');

$app->get('/contact', function() use($app) {
    $app->render("contact.twig");
})->name('contact');

I have not included the form submission code that is covered later.

The bug is interesting. If you start with your base url, the links function correctly. But if you start with your base url/index.php, the home page works, but the links do not because they insert the index.php before the /contact and it is not rendered correctly. You can test it on the link above. I believe a rewrite rule like the ones in the end of the fixing URLs for the Shirts for Mike project refactoring class would solve this, but I have not gone back to try.

Jon Mullins
Jon Mullins
15,724 Points

First, thanks for helping me on this. I compared/copied your code and it looks like mine except for indentation, and it still doesn't work. What's interesting is: If I use http://localhost/templatePHP then click on contact, my url changes to http://localhost/templatePHP/contact but I get a 404.

If I use http://localhost/templatePHP/index.php and click on contact, my url changes to http://localhost/templatePHP/index.php/contact and it renders the contact page, but the CSS is not rendered, just the raw html.

There must be something in another file, that I'm overlooking. I'm almost at the point of moving everything out of my www folder and starting this page over from lesson 1.

Sam Deacon
Sam Deacon
2,650 Points

is it best to build-test on a 'local' linux distro?

i suppose using cloud 9 (ubuntu dev environment ) isn't going to cut it? (and test deploying to heroku would be too time consuming).

I can't dual boot my (family) laptop, I'm considering just buying another cheap one and make a few linux partititions.

Then theres vagrant / docker. hmmmmm

You should post this as a new topic so it is seen. I have never understood the issue with developing in Windows, although I did dual boot this machine and converted another to Linux. Other people may be able to enlighten us both. When you do post, please tag me with @Ted Sumner and select me from the popup.