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 Routing For Our Project

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Routing Error with localhost

Oh well, I was hoping it wouldn't be this soon before I called on the wisdom of Treehouse users again but it looks like I'm stuck again.

I've managed to get the home page working for the application but it won't route properly for the contact page.

<?php 

//require autoload to include all of composers libraries
require "vendor/autoload.php";

// use London time to log errors and warnings
date_default_timezone_set("Europe/London");

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

//create a new instance of monolog
//$log = new Logger('name');   //new monolog logger instance
//$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));  //use object object operator

//$log->addWarning('Warning: ');

$app = new \Slim\Slim();  //new slim object instance

/**/

$app->get('/', function(){

    echo 'Hello, this is the home page.';
});

$app->get('/contact', function(){

    echo 'Feel free to contact us.';
});


//Run the slim application
$app->run();

//echo "Hello, World!";
?>

If I enter localhost/composer/contact I get this

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

         Error 404

localhost
Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12

My code and htaccess file are correct. Any ideas on this one? I realise the videos use workspaces for this but I prefer to use localhost or my own server for this course. :) Thanks

3 Answers

Kevin Korte
Kevin Korte
28,149 Points

I know what your problem is, and good job pushing to git! You did it right, I forked it, and found it pretty fast.

You need to rename your htaccess file to .htaccess. These files always begin with a period in the file name. You'll see that convention used from time to time.

Also, it works without the RewriteBase rule, so I don't know if that's going to screw you up.

Here is my forked copy: https://forked-kevinkorte.c9.io/

And: https://forked-kevinkorte.c9.io/contact

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Sorted, again thanks.:) Feeling accomplished tonight after being pretty down about my progress through this course.

I've just managed to fix the problem in localhost and will work on hosting server tomorrow. Shouldn't be too much of a problem, that.

I should have noticed the filename didn't; have the . to be honest. I just looked at the file in FileZilla, saw it wasn't showing up as a text file and thought i was all set lol But I've just worked out where I went wrong trying to make a htaccess file in notepad++.

All good. :)

Kevin Korte
Kevin Korte
28,149 Points

Have you checked this into git somewhere I can fork the repo and try it myself?

What's your htaccess look like?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Nope, but this is my htaccess file

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

And a link to a copy of the work on my server .

Kevin Korte
Kevin Korte
28,149 Points

I'm kinda shooting in the dark here because I'm not an htaccess pro, but is there are reason the rewritebase rule is commented out. My understanding is that it sets apaches "base" url to the directory this file lives in when it's called, which for this probject, I think you would want at the root of http://www.jonniegrieve.co.uk/jg-lab.co.uk/treehouse/php/composer/

This is an interesting problem. Consider checking this into a public repo on Github?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I have to be honest, I'm not completely down with Git and Github, although I've taken the course but by the end it got way too confusing. It's something I really want to get my head around though.

Anyway I thought the RewiteBase was meant to be commented out?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I've managed (i think) to put my files onto a Github Repo.

https://github.com/jg-digital-media/NewRep

Ignore the &#13 on the end of the link if you see it.

See if you can spot the issue? :)

Rohit Poonia
PLUS
Rohit Poonia
Courses Plus Student 1,698 Points

Hello Jonathan,

Mine code is like this with file structure next to it . Hope this helps

http://prntscr.com/8tqjw0

Best Regards, Rohit

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Rohit.

Thanks for this. It looks like your code is a few videos beyond mine though. :-)