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

Richard Lake
Richard Lake
20,462 Points

workspaces / htaccess / page not found

Okay I have copied the .htaccess file from the teacher's notes as described, used '$app = new \Slim\App();' to instantiate an object. Appended /hello/richard to the address line upon previewing the code and yet... still I continue to see the Page Not Found message.

Richard Lake
Richard Lake
20,462 Points
require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('Europe/London');

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

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

$app = new \Slim\App();

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

$app->run();

?>

---- and .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]```

3 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

The problem might be because Slim has been updated after the course was made. Try check out the Slim Framework website for more information :-)

https://www.slimframework.com/

<?php
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();
Richard Lake
Richard Lake
20,462 Points

Thanks, thats it. I overwrote my code with yours above and it works well. I do notice that also it says {name} as opposed to :name in the get method of the app object so that makes a big difference too.

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

You are going to use the Slim Frameworks website a lot through this course since there's been some changes to Slim since this course was published :-)

Richard Lake
Richard Lake
20,462 Points

Correct. And with that in mind I have decided that the best course of action is to uninstall and install v2.6 instead through composer for the purpose of completing the course.

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

That's an option if you know you wont be using Slim after you finish the course - otherwise I would recommend taking the extra time to learn the updated version on your way through the course since you would have to learn it afterwards anyway :-)

Happy Coding! :-D