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

Pai Boony
PLUS
Pai Boony
Courses Plus Student 4,529 Points

it doesn't work even slim version 2.6

I followed every step from the video include using Slim version 2.6 also .htaccess but when I try to run on my localhost it doesn't work. Can anyone help hear is my code for index.php <?php require 'vendor/autoload.php';

$app = new \Slim\Slim();

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

$app->run();


here is code for .htaccess

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]

5 Answers

Gabor Gazdag
Gabor Gazdag
10,474 Points

Hi if there is a problem with the .htaccess file use the following, it works fine on localhost(XMAP) or on normal server:

RewriteEngine On RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [QSA,L]

I would recommend-if you did not made it so to place every command in a new line.

Regards, Gabor

Pai Boony
Pai Boony
Courses Plus Student 4,529 Points

Thanks Gabor, I re-coding .htaccess as you recommend but still does not work. What should I do?

Gabor Gazdag
Gabor Gazdag
10,474 Points

Hi what kind of error are you receiving?

Frank Ruiz
Frank Ruiz
15,558 Points

If you downloaded the most recent version of Slim, you need to use the most recent 'hello world' example from their documentation. Try this:

$app = new \Slim\App($config);

// Define app routes $app->get('/hello/{name}', function ($request, $response, $args) { return $response->write("Hello " . $args['name']); });

// Run app $app->run();

samuel linus
samuel linus
1,135 Points

Note: Don't forget to add the use, that will make it work. I feel the video needs to be updated.

use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App; $app->get('/hello/{name}', function (Request $request, Response $response) { $name = $request->getAttribute('name'); $response->getBody()->write("Hello, $name");

return $response;

}); $app->run();

Abraham Juliot
Abraham Juliot
47,353 Points

Thank you samuel linus. Your solution worked.

Emilze Eloy
Emilze Eloy
2,993 Points

It's not working for me as well. Slim 3.0.

Gabor Gazdag
Gabor Gazdag
10,474 Points

Hi. Slim3 is working a bit different from Slim2. Do you receiving an error or it is just nothing happens? It would be nice to see your code as well.

Emilze Eloy
Emilze Eloy
2,993 Points

I changed to Slim 2.3 and it worked.

I found what I was doing wrong after watching this YT tutorial:

https://www.youtube.com/watch?v=MSNYzz4Khuk

Now my code works... and this treehouse video is also right, just a little bit outdated. but it should work anyways.... I was making a noob mistake when creating the htaccess... it has to look like this: http://imgur.com/a/fwSVr

Abraham Juliot
Abraham Juliot
47,353 Points

This worked for me --- using slim 3.0

require __DIR__ . '/vendor/autoload.php';

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");
    return $response; 
});

$app->run();