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

Building Websites with PHP/ Including & Running Slim. Not finding the Hello World section on the Slim User Guide.

Not finding the Hello World section in the Slim User Guide. My screen does not look like the one in the video. Guessing site has been updated???

My code typed in:

<?php
require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set ('America/New_York');

//$log = new Monolog\Logger('name');
//$log->pushHandler(new Monolog\Handler\StreamHandler('app.txt', Monolog\Logger::WARNING));
//$log->addWarning('Oh Noes');

$app = new \Slim\Slim();

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

$app->run();

getting error message on preview: atal error: Uncaught Error: Class 'Slim\Slim' not found in /home/treehouse/workspace/index.php:9 Stack trace: #0 {main} thrown in /home/treehouse/workspace/index.php on line 9

8 Answers

Maximillian Fox
PLUS
Maximillian Fox
Courses Plus Student 9,236 Points

Correct - slim is now in its version 3.x iteration.

There is documentation for slim 2.x on their site still, the best thing to do is in composer download and install slim 2.6 (as composer will let you select the version you want to use).

Then you should be able to follow along the tutorials. I think Hampton needs to update this one to 3.x as this would be awesome to have it up to date and learning on its current version.

Baur Alzhanov
Baur Alzhanov
7,028 Points

Thank you for your attention. I was add "2.6" in composer.json; But it haven't still worked. Then I change my code for version 3.3 SLIM. And "Hello, friend" is working! Yahhoooo...

<?php
// Create and configure Slim app
$app = new \Slim\App;

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

// Run app
$app->run();
Alena Holligan
Alena Holligan
Treehouse Teacher

For Slim 2, you need to make sure you also have the .htaccess file or /hello/name will give you a 404

Mike Ribera
Mike Ribera
4,127 Points

Using your code, thanks.

Baur Alzhanov
Baur Alzhanov
7,028 Points

Mike, I think version Slim 2.6 is better, because code string is shorter.

Thanks! Downloaded slim 2.6 and was able to follow instructions properly once I found the v 2 instructions too. Would be nice to have a type or something on video saying download 2.6 not most recent version.

Baur Alzhanov
Baur Alzhanov
7,028 Points

Hi Carla. Can you help me. How you can change version of SLIM? I have had this problem too.

Alena Holligan
Alena Holligan
Treehouse Teacher

change your composer.json to remove the "^". The line should show

"slim/slim": "2.6"
Baur Alzhanov
Baur Alzhanov
7,028 Points

Pages are working. If I include /hello/name I can see "Hello name". When I didn't include anything so I saw message "Page Not Found".

Alena Holligan
Alena Holligan
Treehouse Teacher

you don't have a route for $app->get('/') so only /hello/Baur will work right not continue on with the course and you should figure it out.

Baur Alzhanov
Baur Alzhanov
7,028 Points

In the next stage "Routing For Our Project" I wrote this code:

<?php
require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('Asia/Almaty');

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

$app = new \Slim\App;

// Define app routes
$app->get('/', function () {
    echo 'Hello, this is the home page.';
});

$app->get('/contact', function () {
    echo 'Fell free to contact us.';
});

// Run app
$app->run();
Mike Ribera
Mike Ribera
4,127 Points

We really need new videos using Slim 3.

Mike Ribera
Mike Ribera
4,127 Points

Forget Slim 3, 2.6 is really better - and it's a shame.