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 trialPai Boony
Courses Plus Student 4,529 Pointsit 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
10,474 PointsHi 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
Frank Ruiz
15,558 PointsIf 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
1,135 PointsNote: 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
47,353 PointsThank you samuel linus. Your solution worked.
Emilze Eloy
2,993 PointsIt's not working for me as well. Slim 3.0.
Gabor Gazdag
10,474 PointsHi. 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
2,993 PointsI changed to Slim 2.3 and it worked.
Hector F.
27,464 PointsI 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
47,353 PointsThis 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();
Pai Boony
Courses Plus Student 4,529 PointsPai Boony
Courses Plus Student 4,529 PointsThanks Gabor, I re-coding .htaccess as you recommend but still does not work. What should I do?
Gabor Gazdag
10,474 PointsGabor Gazdag
10,474 PointsHi what kind of error are you receiving?