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

Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

htaccess file in xampp

I created index.php page with the following code:

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

I am using xampp server but when I run the file, it is showing 404 error. I also used the htaccess code shown in the video but can't find the fix. Need a Help!

Are you running apache? And is your index.php page in htdocs files?

You're solution is probably like this one:

https://teamtreehouse.com/forum/i-cant-access-my-local-host

Check it and tell me if this solution worked.

Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

I have tried this earlier. I am following the steps shown in the Hampton's video.

  1. First I created .json file.
  2. Then, I installed monolog/monolog
  3. After installing slim, when I use $app->run(); it is throwing the 404 error
  4. Since I cannot create .htaccess file from txt file, I copied the file that was in \Slim\Slim.htaccess and pasted it in htdocs where I have my index.php

Basically, routing is not working in my case I guess

try this then

require('/vendor/autoload.php');

Note: I added (...) to your code

Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

Still having the same problem

In your result page, check the element inspector for errors. ( CTRL + Shift + I )

See if in the network the connection to your index.php is established and the erros in the console.

Past here the errors after please.

Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

No, The connection ain't established. If I echo any statement without using Slim commands, then I can get the output. But if I use following code, then it ain't working

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

If I use the following code then it is working

<?php 
require '/vendor/autoload.php';

echo "Hello World!";
?>

Let's try this. On the console, see how many works you've received.

However, I think that the problem is the syntax in new\Slim\Slim();

Try this first:

$app = new slim slim();

If it now work, do the following in order to get more information

<?php 

require('/vendor/autoload.php');

console.log("work-1");

$app = new \Slim\Slim(); 

console.log("work-2");

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

console.log("work-3");
     echo "Hello, " . $name; 
console.log("work-4");
}); 

$app->run();

console.log("work-5");
?>
Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

No it ain't working yet. I think the problem is in creating the htaccess file. It can't link the slim library files and hence it is not establishing connection.

I've read some docs ( http://docs.slimframework.com/ ) and have you instaled Slim like it is in the instructions ( via composer )?

If you have, i can only assume that the problem is in the autoload.php file. ( See if the path is correct... try putting all the path, from the beggining to the end e.g.: C:\.....\autoload.php )

Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

Yes I have installed slim framework using composer. Also, autoload.php is working because when I put log files, it is displaying. I cannot figure out the problem

Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

Thanks a lot @Bruno. I have figured it out and it is all working fine now

I was going to instaled and test by myself to try a thing.

So, what was the solution?

Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

It will sound funny but I put the .htaccess file in vendor folder. On changing the file location to index.php location, It started working

I thought that they were together.

Still, it's ok. Every programer have already had this type of problem.

Continue the good work!

Rutvij Kanzaria
Rutvij Kanzaria
19,624 Points

Yeah Thank You. I just have one more doubt. Is the content of every .htaccess file same for every project?