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 & Running Slim

Michael Rockett
Michael Rockett
40,365 Points

/hello/name not working still shows 404 page

Hi, I'm still only getting the 404 page at the moment when I add /hello/name to the url. I've adding the .htaccess as well.

I should add I'm not doing this in workspaces I'm doing it on my computer and php is working fine

Here is my code

<?php

require 'vendor/autoload.php'; date_default_timezone_set("Europe/London");

//$log = new Logger('name'); //$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING)); //$log->addWarning('Foo');

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) { echo "Hello, $name"; });

$app->run();

?>

Thanks, Mike

In the future please format your code following the markdown cheatsheet linked below.

<?php

require 'vendor/autoload.php'; 
date_default_timezone_set("Europe/London");

//$log = new Logger('name'); 
//$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING)); 
//$log->addWarning('Foo');

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) { 
echo "Hello, $name"; });

$app->run();

?>

10 Answers

Michael Rockett
Michael Rockett
40,365 Points

Thanks for the help but I've solved the issue now. It was really simple. I called the project php composer practice which didn't work as this caused spaces in the url. I've renamed the project with no spaces and it now works fine

Thanks, Mike

Any time. You can probably guess by my questions that I was looking for a server problem.

michael7williams
michael7williams
9,638 Points

This solved my issues in wamp too. I think you could supplement the %'s for spaces in the url and it will work as well.

did you do the steps in the video starting at 3:00? They also get a 404 error and show you what to do to get a result with that code.

Michael Rockett
Michael Rockett
40,365 Points

Yeah I did that, I added hello and then a name but I still get 404. In the video however at this point they get not found which means I must be doing something wrong before then.

Will you post a snapshot? Use the camera icon in the upper right of the screen to create the snapshot. Post the link it creates.

Michael Rockett
Michael Rockett
40,365 Points

I'm not sure how to take a snap shot and can't find the camera icon (Im on a pc) but this is the url http://localhost/php%20Composer%20Practice/hello/mike

Michael Rockett
Michael Rockett
40,365 Points

and this is what I get...

404 Page Not Found

The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below. Visit the Home Page

You are not in Workspaces. My bad.

Did the monolog work?

Michael Rockett
Michael Rockett
40,365 Points

I think so it's in my vendor folder

Did you create the htaccess file? Many text editors will not open the file as a .htaccess, so it is created without the period. The you have to rename the file .htaccess and ensure it is in the project root file.

Michael Rockett
Michael Rockett
40,365 Points

yep I did that, I added the code while it was a htaccess.txt file then renamed it to .htaccess

Please post the contents of your .htaccess file.

Michael Rockett
Michael Rockett
40,365 Points

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]

Michael Rockett
Michael Rockett
40,365 Points

Sorry I forgot to use the markdown sheet

No problem. It looks right to me, but I am not an expert on this.

I assume you use XAMPP. Is it running? If not, is whatever you are using as server software running?

Michael Rockett
Michael Rockett
40,365 Points

Yes I'm using XAMPP and it's running apache

Did Monolog work when you did that part of the course?

Michael Rockett
Michael Rockett
40,365 Points

Hi, I found that my problem was that there were spaces in the name of my urlso it was like php practice/hello/myname. When I changed the project name to phppractice the url then had no spaces and this fixed it for me. Could this be the same problem your having?

Sajid Latif
seal-mask
.a{fill-rule:evenodd;}techdegree
Sajid Latif
Full Stack JavaScript Techdegree Student 22,368 Points

Yes, the monolog is working, since the 404 error is appearing, which is great. But the concern is about the name, which I put in the url after the /Hello/myname.

Ive made the exact same course which hampton did and I had the problem there. Now Im doing it on my live website, where I wont to use slim, BUT the exact same problem is showing with the /Hello/myname so I was wondering if it is the .htaccess file.

MIchael. Ive tried with differnt urls. :(

We need to see code. A code snapshot if you are working in Workspaces or copy and paste. Please use the Markdown Cheatsheet formatting for copied code. A workspace snapshot is made with the camera icon in the upper right of the workspace.

so the issue you have is only with the contact page?

Because the whole site is working except the link to the page, we know the error is somewhere in the link. In this case I was able to correct the problem by changing line 30 on main.twig to:

<?php
<a href="{{siteUrl('/contact')}}">Contact</a>

I did not play with the contact name to figure out what was wrong, but this works.

If you have finished the course you missed making this change and the change to the About link. You will want to find the video on that. The About link should be:

<?php
<a href="{{ baseUrl()}}" class="selected">About</a>

If you have not finished the course, keep going.

Sajid Latif
seal-mask
.a{fill-rule:evenodd;}techdegree
Sajid Latif
Full Stack JavaScript Techdegree Student 22,368 Points

Thanks for your fast reply. I appriciate it a lot :)

Now this work. But do you know why this doesnt work for me? I try to insert my name in URL

/Hello/myname

but it doesn not show my name, but the 404 error. Thats strange. Ive also tried it on my live website and still the same. Maybe you have the answer?

<?php
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});
$app->run();

Ive made a snapshot of it.

https://w.trhou.se/cy6lrk6dvu

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Try to visit /hello/myname instead /Hello/myname